Comments (5)
I have run some preliminary experiments on conversion using jax2tf. It semi-works, however in certain cases hits JAX error.
Following example works:
wrn_width = 2 # Width of WideResNet
wrn_depth = 28 # Depth of WideResNet
batch_size = 4
# Model
model = WideResNet(nin=3, nclass=10, depth=wrn_depth, width=wrn_width)
model_vars = model.vars()
# Prediction operation
predict_op = lambda x: objax.functional.softmax(model(x, training=False))
predict_op = objax.Jit(predict_op, model_vars)
# Run prediction on random batch
x = objax.random.normal((batch_size, 3, 32, 32))
pred_y = predict_op(x)
print(pred_y)
# Convert model to Tensorflow and run it on the same batch
predict_tf = jax2tf.convert(predict_op)
print(predict_tf(np.array(x)))
However, attempt to re-run predict_tf
one more time causes UnexpectedTracerError: Encountered an unexpected tracer. Perhaps this tracer escaped through global state from a previously traced function.
. Which is probably related to the issue we observing in #158
from objax.
I don't see how it could escape though, it looks like something specific to jax2tf somehow.
Otherwise the error would also show up when running predict_op
twice (the JAX jitted one).
from objax.
I guess you're right. Maybe I have to add functional wrapper for jax2tf (similar to Jit, Grad, etc...). I'll look into this more
from objax.
After further investigation I made a version which seems to be working without issues:
class Objax2Tf(tf.Module):
def __init__(self, module: objax.Module):
assert isinstance(module, objax.Module), 'Input argument to Objax2Tf must be an Objax module.'
module_vars = module.vars()
def wrapped_op(tensor_list: List[JaxArray], kwargs, *args):
original_values = module_vars.tensors()
try:
module_vars.assign(tensor_list)
return module(*args, **kwargs)
finally:
module_vars.assign(original_values)
tf_function = jax2tf.convert(wrapped_op)
self._all_vars = [tf.Variable(v) for v in module_vars.tensors()]
self._call = tf.function(
lambda *args, **kwargs: tf_function(self._all_vars, kwargs, *args),
autograph=False)
def __call__(self, *args, **kwargs):
return self._call(*args, **kwargs)
predict_tf = Objax2Tf(predict_op)
print(predict_tf(np.array(x)))
It also could be saved and loaded as Tensorflow SavedModel.
Still need to do more testing of various corner cases.
from objax.
Objax2Tf
converter is implemented.
There are some follow up improvement which will be tracked in other issues.
from objax.
Related Issues (20)
- Improve automatic variable tracing
- If user won't add random generator to VarCollection of jitted code then same number always return by random generator
- Model compiling twice when using jax==0.2.10 or later HOT 6
- Update Objax Basics tutorial to reflect .value change
- Accessing variable of a vectorized module HOT 2
- objax.Jit reports error when StateVar is added to the vc argument HOT 3
- Regression of JAX duck typing. HOT 2
- RecursionError when attempting to unpickle objax objects HOT 2
- How to compute Jacobian of outputs w.r.t. inputs HOT 3
- Activation functions like Swish and Mish are absent. HOT 1
- Error due to the deprecation of jax.api
- objax.Jacobian and objax.Hessian similar to objax.Grad HOT 4
- replacing jax.vmap with objax.Vectorize HOT 3
- Closure scoping for nested objax.Functions HOT 2
- ResNetV2 from model.zoo does not specify train arg for ResNetV2Block HOT 2
- pmean inside objax.parallel causes multithreading deadlock for more than 2 gpus HOT 3
- `objax.variable.VarCollection.update` fails when passing `Dict[str, Any]` HOT 1
- `objax.variable.VarCollection.update` not compliant with key-value assignment HOT 1
- TypeError during gradient computation: type <class 'objax.variable.TrainVar'> is not a valid JAX type
- Missing release 1.7.0 on GitHub HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from objax.