question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

size of slices can’t be functions of argument values

See original GitHub issue

I’m having problems implementing the following fori_loop body.

def body_fun(i, val):
    batch_sizes, packed_input_data, input_offset = val

    batch_size = packed_input.batch_sizes[i]

    #batch = packed_input_data[input_offset:input_offset+batch_size]
    batch = dynamic_slice_in_dim(packed_input_data, input_offset, batch_size)
    input_offset += batch_size

    return batch_sizes, packed_input_data, input_offset

I’m trying to replace the commented out code with jax primitives - i.e. slicing packed_input_data (n x m) based on the input_offset[i] (int). The issue I’m running into is dynamic_slice_in_dim throws:

TypeError: Abstract value passed to int, which requires a concrete value. Try using value.astype(int) instead.

What seems to be happening is dynamic_slice_in_dim attempts to cast input_offset and/or batch_size as int() which throws since they’re traced and don’t support this cast? i.e. If I use literal ints for the start_index, slice_size it works. Also if I call body_fun(i, val) from a pure python for loop it also works no problems.

How should I approach this problem? I can paste full code if required, my full implementation also includes packed_output_data (same shape as packed_input_data) which I’m trying to update using dynamic_update_slice plus a state array which also slice by batch_size.

(I’m trying to implement variable length sequence RNN’s using PyTorch as inspiration.)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
david-waterworthcommented, Feb 28, 2020

I’ve read through https://github.com/google/jax/issues/2308 which seems similar but I’m still cannot get this to work.

No matter what primitive I use to translate the 2nd slice operation below it fails with an exception that seems to imply batch_size must be a concrete value but an abstract value was supplied.

input_offset = 0
for i in range(batch_sizes.shape[0]):
    batch_size = batch_sizes[i]
    batch = packed_input_data[input_offset:input_offset+batch_size,:]  # issue here
    input_offset = input_offset + batch_size
0reactions
david-waterworthcommented, Mar 9, 2020

Yes thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass slice as function argument, and modify the original slice
I know everything is passed by value in Go, meaning if I give a slice to a function and that function appends to...
Read more >
Pass slice as a function argument · golang-101-hacks - nanxiao
In Go , the function parameters are passed by value. ... the starting address of the underlying array, accompanied by the length and...
Read more >
Golang tips: why pointers to slices are useful and ... - Medium
The pointer to a slice is indispensable when the function is going to modify the structure, the size, or the location in memory...
Read more >
How To Use Variadic Functions in Go - DigitalOcean
This tells Go that the function can accept zero, one, or many arguments. The sayHello function receives the names parameter as a slice...
Read more >
The Sharp Bits — JAX documentation
JAX re-runs the Python function when the type or shape of the argument ... or lax.fori_loop the size of slices can't be functions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found