Skip to content

tf.constant(np_array) loses numpy dtype/shape, producing ⊤ for downstream consumers #539

@khatchad

Description

@khatchad

Summary

tf.constant(np_array) does not propagate the numpy array's dtype to the resulting tensor in Ariadne's inference. Any consumer of the resulting tensor sees ⊤ shape / UNKNOWN dtype.

Reproduction

Minimal fixture tf2_test_constant_from_numpy.py (on branch ml-22-empirical-top-p-logits, commit follows this issue):

import numpy as np
import tensorflow as tf

def consume(x):
    return x

arr = tf.constant(np.ones((2, 3), dtype=np.float32))
consume(arr)

At runtime, arr.shape == (2, 3) and arr.dtype == tf.float32. Ariadne infers consume's parameter x as:

vn=2 -> [{? of unknown}]

— both shape and dtype are ⊤, despite the explicit dtype=np.float32 on the numpy array and despite tf.constant preserving dtype faithfully at runtime.

Test: com.ibm.wala.cast.python.ml.test.TestTensorflow2Model.testConstantFromNumpy.

Impact

This pattern (tf.constant(np.ones / np.array / np.zeros(..., dtype=...))) is common in TF tutorial code and the perf-eval corpus. Surfaced during the input-signature-inference empirical pass (ponder-lab/Input-Signature-Inference-Paper#22) on YunYang1994/TensorFlow2.0-Examples/2-Basical_Models/Multilayer_Perceptron.py's multilayer_perceptron(x) function, which receives its parameter from a tf.constant(np.ones(...)) caller. With this bug, multilayer_perceptron(x) is the first load-bearing UNKNOWN-dtype function the empirical pass has identified — input-signature emission would fail because dtype is required.

This is a contributory blocker for input-signature inference on the perf-eval corpus.

Likely Cause

Ariadne's modeling of tf.constant (in tensorflow.xml) probably treats the argument as opaque rather than:

  1. Recognizing that the input is a numpy array.
  2. Reading the numpy array's dtype attribute (or shape).
  3. Plumbing those into the resulting tensor's type.

The numpy-array → tensor type bridge needs first-class modeling.

Suggested Fix

Add a TensorGenerator (or extend an existing one for tf.constant) that:

  1. Detects when the value argument is a numpy ndarray construction (np.ones(...), np.zeros(...), np.array(...)).
  2. Extracts the numpy dtype keyword argument (or default).
  3. Maps to Ariadne's DType enum.
  4. Extracts the shape argument from the numpy constructor for shape inference.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions