def render_tensor_sandbox(hide_function_defs: bool): st.write("## Sandbox for Tensors") st.write("**Define your tensor**") # Consistent random number generator rng = np.random.RandomState(42) # col1, col2 = st.beta_columns(2) tensor_shape = st_eval_error_message( st.text_input("Tensor shape", value="(2, 2, 2)"), "Tensor shape must be defined as an in-line tuple, i.e. (2, 2, 2)", ) tensor_size = int(operators.prod(tensor_shape)) random_tensor = st.checkbox("Fill tensor with random numbers", value=True) if random_tensor: tensor_data = np.round(rng.rand(tensor_size), 2) st.write("**Tensor data storage:**") # Visualize horizontally st.write(tensor_data.reshape(1, -1)) else: tensor_data = st_eval_error_message( st.text_input("Tensor data storage", value=str(list(range(tensor_size)))), "Tensor data storage must be defined as an in-line list, i.e. [1, 2, 3, 4]", ) try: test_tensor = Tensor.make(tensor_data, tensor_shape, backend=TensorFunctions) except AssertionError as e: storage_size = len(tensor_data) if tensor_size != storage_size: st.error( f"Tensor data storage must define all values in shape ({tensor_size} != {storage_size })" ) else: st.error(e) return select_fn = { "Visualize Tensor Definition": interface_visualize_tensor, "Visualize Tensor Strides": interface_strides, "function: index_to_position": interface_index_to_position, "function: to_index": interface_to_index, "function: TensorData.permute": interface_permute, } selected_fn = st.selectbox("Select an interface", options=list(select_fn.keys())) select_fn[selected_fn](test_tensor, hide_function_defs)
def test_prod(x, y, z): assert_close(operators.prod([x, y, z]), x * y * z)
def test_prod(x, y, z): assert_close(prod([x, y, z]), x * y * z)