TensorTonic
Problems
Functional Array Update
JAX
Easy

Add a batch of weighted updates into a histogram and return the new histogram. JAX arrays cannot be changed in place, so the result has to be built as a new array.

counts holds the starting value of each of the K bins. Update number n adds weights[n] to the bin whose number is indices[n], so for every bin k

resultk=countsk+∑n : indicesn=kweightsn\text{result}_k = \text{counts}_k + \sum_{n \,:\, \text{indices}_n = k} \text{weights}_nresultk​=countsk​+n:indicesn​=k∑​weightsn​

where the sum runs over every update n whose index equals k. The same bin number can appear many times in indices, and every one of those updates must be counted. An empty indices array leaves the counts as they are.

Only the indices 0 to K - 1 name a bin. Some updates carry an index outside that range, for example -1 for an item that an earlier step discarded, or K for an item past the last bin. Every such update, negative or too large, must be ignored and changes no bin.

Your function is called under jax.jit, so its inputs are traced arrays whose values are unknown while the function is being compiled.

Return a new float32 JAX array of shape (K,).

Examples

Input: counts = [0.0,0.0,0.0,0.0], indices = [1,3,1,0], weights = [1.0,1.0,1.0,1.0]

Output: [1.0,2.0,0.0,1.0]

Explanation: Bin 1 appears twice in indices, so it receives two weights of 1. Bins 0 and 3 receive one weight each, and bin 2 receives none.

Input: counts = [0.5,0.0,2.0], indices = [2,-1,2,3,0], weights = [0.25,0.5,1.0,4.0,-1.0]

Output: [-0.5,0.0,3.25]

Explanation: The indices -1 and 3 lie outside the three bins, so their weights 0.5 and 4.0 are ignored. Bin 2 receives 0.25 and 1.0 on top of its starting 2.0, and bin 0 receives -1.0.

Input: counts = [1.0,2.0,3.0], indices = [], weights = []

Output: [1.0,2.0,3.0]

Hint 1

counts.at[indices].add(weights) adds every weight into its bin, and repeated indices accumulate.

Hint 2

The .at methods take mode="drop", which skips indices past the end but still wraps negative ones.

Hint 3

jnp.where(valid, weights, 0.0) zeroes the weights you want to ignore.

Requirements

  • Build the result from JAX array operations without converting the inputs to NumPy arrays or Python numbers.

Constraints

  • counts is a float32 JAX array of shape (K,) with K between 1 and 12.
  • indices is an int32 JAX array of shape (N,) with N between 0 and 20, and every entry lies between -2 and K + 1.
  • weights is a float32 JAX array of shape (N,), holding one weight per index.
  • All values are finite.
Try Similar Problems
PRNG Key SplittingEasyMasked Mean Without Boolean IndexingEasyPad and Mask to Bucket LengthMediumValue and Grad of a LossEasyStatic Arguments in jitEasyModel Ensemble with in_axesEasySGD with Momentum over a PytreeEasyRotary Position EmbeddingEasyKV Cache Update at a PositionEasyDevice Mesh and Named ShardingEasyHessian-Vector ProductMediumPer-Example Gradient ClippingMedium

Accepts: array

Accepts: array

Accepts: array

You must run your code first.
Functional Array Update

Functional Array Update

JAX
Easy

Add a batch of weighted updates into a histogram and return the new histogram. JAX arrays cannot be changed in place, so the result has to be built as a new array.

counts holds the starting value of each of the K bins. Update number n adds weights[n] to the bin whose number is indices[n], so for every bin k

resultk=countsk+∑n : indicesn=kweightsn\text{result}_k = \text{counts}_k + \sum_{n \,:\, \text{indices}_n = k} \text{weights}_nresultk​=countsk​+n:indicesn​=k∑​weightsn​

where the sum runs over every update n whose index equals k. The same bin number can appear many times in indices, and every one of those updates must be counted. An empty indices array leaves the counts as they are.

Only the indices 0 to K - 1 name a bin. Some updates carry an index outside that range, for example -1 for an item that an earlier step discarded, or K for an item past the last bin. Every such update, negative or too large, must be ignored and changes no bin.

Your function is called under jax.jit, so its inputs are traced arrays whose values are unknown while the function is being compiled.

Return a new float32 JAX array of shape (K,).

Examples

Input: counts = [0.0,0.0,0.0,0.0], indices = [1,3,1,0], weights = [1.0,1.0,1.0,1.0]

Output: [1.0,2.0,0.0,1.0]

Explanation: Bin 1 appears twice in indices, so it receives two weights of 1. Bins 0 and 3 receive one weight each, and bin 2 receives none.

Input: counts = [0.5,0.0,2.0], indices = [2,-1,2,3,0], weights = [0.25,0.5,1.0,4.0,-1.0]

Output: [-0.5,0.0,3.25]

Explanation: The indices -1 and 3 lie outside the three bins, so their weights 0.5 and 4.0 are ignored. Bin 2 receives 0.25 and 1.0 on top of its starting 2.0, and bin 0 receives -1.0.

Input: counts = [1.0,2.0,3.0], indices = [], weights = []

Output: [1.0,2.0,3.0]

Hint 1

counts.at[indices].add(weights) adds every weight into its bin, and repeated indices accumulate.

Hint 2

The .at methods take mode="drop", which skips indices past the end but still wraps negative ones.

Hint 3

jnp.where(valid, weights, 0.0) zeroes the weights you want to ignore.

Requirements

  • Build the result from JAX array operations without converting the inputs to NumPy arrays or Python numbers.

Constraints

  • counts is a float32 JAX array of shape (K,) with K between 1 and 12.
  • indices is an int32 JAX array of shape (N,) with N between 0 and 20, and every entry lies between -2 and K + 1.
  • weights is a float32 JAX array of shape (N,), holding one weight per index.
  • All values are finite.
Try Similar Problems
PRNG Key SplittingEasyMasked Mean Without Boolean IndexingEasyPad and Mask to Bucket LengthMediumValue and Grad of a LossEasyStatic Arguments in jitEasyModel Ensemble with in_axesEasySGD with Momentum over a PytreeEasyRotary Position EmbeddingEasyKV Cache Update at a PositionEasyDevice Mesh and Named ShardingEasyHessian-Vector ProductMediumPer-Example Gradient ClippingMedium

Accepts: array

Accepts: array

Accepts: array

You must run your code first.
Problems
Loading...
1 / 1