{"record":{"id":"02d3bb67f642eb18","repo":"jax-ml/jax","slug":"jnp-fromfile-is-not-implemented-because-it-may-b","errorCode":null,"errorMessage":"jnp.fromfile() is not implemented because it may be non-pure and thus unsafe for use with JIT and other JAX transformations. Consider using jnp.asarray(np.fromfile(...)) instead, although care should be taken if np.fromfile is used within a jax transformations because of its potential side-effect of consuming the file object; for more information see https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html#pure-functions","messagePattern":"jnp\\.fromfile\\(\\) is not implemented because it may be non-pure and thus unsafe for use with JIT and other JAX transformations\\. Consider using jnp\\.asarray\\(np\\.fromfile\\(\\.\\.\\.\\)\\) instead, although care should be taken if np\\.fromfile is used within a jax transformations because of its potential side-effect of consuming the file object; for more information see https://docs\\.jax\\.dev/en/latest/notebooks/Common_Gotchas_in_JAX\\.html#pure-functions","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":5532,"sourceCode":"    Array([0, 1, 2, 3, 4], dtype=int32)\n\n  .. _Python buffer interface: https://docs.python.org/3/c-api/buffer.html\n  \"\"\"\n  return asarray(np.frombuffer(buffer=buffer, dtype=dtype, count=count, offset=offset))\n\n\n@export\ndef fromfile(*args, **kwargs):\n  \"\"\"Unimplemented JAX wrapper for jnp.fromfile.\n\n  This function is left deliberately unimplemented because it may be non-pure and thus\n  unsafe for use with JIT and other JAX transformations. Consider using\n  ``jnp.asarray(np.fromfile(...))`` instead, although care should be taken if ``np.fromfile``\n  is used within jax transformations because of its potential side-effect of consuming the\n  file object; for more information see `Common Gotchas: Pure Functions\n  <https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html#pure-functions>`_.\n  \"\"\"\n  raise NotImplementedError(\n    \"jnp.fromfile() is not implemented because it may be non-pure and thus unsafe for use \"\n    \"with JIT and other JAX transformations. Consider using jnp.asarray(np.fromfile(...)) \"\n    \"instead, although care should be taken if np.fromfile is used within a jax transformations \"\n    \"because of its potential side-effect of consuming the file object; for more information see \"\n    \"https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html#pure-functions\")\n\n\n@export\ndef fromiter(*args, **kwargs):\n  \"\"\"Unimplemented JAX wrapper for jnp.fromiter.\n\n  This function is left deliberately unimplemented because it may be non-pure and thus\n  unsafe for use with JIT and other JAX transformations. Consider using\n  ``jnp.asarray(np.fromiter(...))`` instead, although care should be taken if ``np.fromiter``\n  is used within jax transformations because of its potential side-effect of consuming the\n  iterable object; for more information see `Common Gotchas: Pure Functions\n  <https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html#pure-functions>`_.\n  \"\"\"","sourceCodeStart":5514,"sourceCodeEnd":5550,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L5514-L5550","documentation":"jnp.fromfile is intentionally not implemented in JAX because reading from a file object is a side effect (it consumes the file), violating the purity requirements of JIT and other transformations. The error message directs users to load data with numpy first and convert via jnp.asarray.","triggerScenarios":"Any call to jnp.fromfile(file, dtype=...) — it unconditionally raises NotImplementedError. Commonly hit when porting NumPy code that loads binary data (e.g. MNIST images, .npy raw dumps) directly.","commonSituations":"Ported np.fromfile pipelines for binary datasets; attempting to load data inside a jitted function or during sharded initialization where the file read would be replicated and impure.","solutions":["Replace with jnp.asarray(np.fromfile(path, dtype))","Better: use np.load / np.memmap for .npy files, then jnp.asarray","Ensure loading happens outside jax.jit / pmap and other transformations"],"exampleFix":"// before\nx = jnp.fromfile('data.bin', dtype=np.float32)\n// after\nx = jnp.asarray(np.fromfile('data.bin', dtype=np.float32))","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Grep for jnp.fromfile when upgrading/porting; replace with jnp.asarray(np.fromfile(...))","Keep all file I/O outside jitted functions","Use np.load/np.memmap for structured files"],"tags":["jax","fromfile","notimplemented","io","jit-purity"],"backgroundTag":"unsupported-api-in-jax","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}