def testIsEvenTypeInferenceTF(self, dtype): for inputs, outputs in [([1], [False]), ([5, 6, 0, 3], [False, True, True, False])]: inputs = np.array(inputs, dtype=dtype) outputs = np.array(outputs, dtype=np.bool) tf.compat.v1.logging.debug('tf.even {} {} {}'.format( dtype, inputs.shape, outputs.shape)) inputs_t = tf.constant(inputs, dtype=dtype) prog = test_programs.is_even_function_calls(include_types=False) typed = type_inference.infer_types(prog, [inputs_t], TF_BACKEND) expected_prog = test_programs.is_even_function_calls(dtype=dtype) self.assertSameTypes(expected_prog, typed) alloc = allocation_strategy.optimize(typed) lowered = lowering.lower_function_calls(alloc) self.assertAllEqual( outputs, self.evaluate( _execute(lowered, inputs_t, int(np.max(inputs)) + 3, TF_BACKEND)))
def testAllocatingIsEvenProgram(self): prog = test_programs.is_even_function_calls() answer = { inst.pc_var: inst.VariableAllocation.FULL, 'ans': inst.VariableAllocation.REGISTER, 'cond': inst.VariableAllocation.REGISTER, 'n': inst.VariableAllocation.REGISTER, 'n1': inst.VariableAllocation.REGISTER, 'nm1': inst.VariableAllocation.FULL } self.assertAllocates(answer, prog)
def testAllocatingIsEvenProgramNoPops(self): prog = test_programs.is_even_function_calls() strip_pop_ops(prog) answer = { inst.pc_var: inst.VariableAllocation.FULL, 'ans': inst.VariableAllocation.REGISTER, 'cond': inst.VariableAllocation.REGISTER, 'n': inst.VariableAllocation.REGISTER, 'n1': inst.VariableAllocation.REGISTER, 'nm1': inst.VariableAllocation.TEMPORARY } self.assertAllocates(answer, prog)
def testIsEvenTypeInferenceNumpy(self, dtype): for inputs, outputs in [([1], [False]), ([5, 6, 0, 3], [False, True, True, False])]: inputs = np.array(inputs, dtype=dtype) outputs = np.array(outputs, dtype=np.bool) tf.compat.v1.logging.debug('np.even {} {} {}'.format( dtype, inputs.shape, outputs.shape)) prog = test_programs.is_even_function_calls(include_types=False) typed = type_inference.infer_types(prog, [inputs], NP_BACKEND) expected_prog = test_programs.is_even_function_calls(dtype=dtype) # We can only assert on the int64/float64 cases because numpy does # not match-cast types on arithmetic with constants. # i.e. (np.int32(0) - 1).dtype == np.int64 self.assertSameTypes(expected_prog, typed, check_dtypes=dtype(0).nbytes == 8) alloc = allocation_strategy.optimize(typed) lowered = lowering.lower_function_calls(alloc) self.assertAllEqual( outputs, _execute(lowered, inputs, int(np.max(inputs)) + 3, NP_BACKEND))
def _is_even_stackless_execute(inputs, backend): prog = test_programs.is_even_function_calls() alloc = allocation_strategy.optimize(prog) return stackless.execute(alloc, backend, None, inputs)