24 """Send the `value` - currently just a scalar - formatted as per `spec`."""
26 if spec.element_type == ctypes.c_int64:
27 to_send = ctypes.c_int64(int(value))
28 elif spec.element_type == ctypes.c_float:
29 to_send = ctypes.c_float(float(value))
31 raise ValueError(f
"unsupported advice element type {spec.element_type}")
32 assert f.write(bytes(to_send)) == ctypes.sizeof(spec.element_type) * math.prod(
41 process_and_args: List[str],
45 temp_rootname: the base file name from which to construct the 2 pipes for
46 communicating with the compiler.
47 make_response: a function that, given the current tensor values, provides a
49 process_and_args: the full commandline for the compiler. It it assumed it
50 contains a flag poiting to `temp_rootname` so that the InteractiveModeRunner
51 would attempt communication on the same pair as this function opens.
53 This function sets up the communication with the compiler - via 2 files named
54 `temp_rootname`.in and `temp_rootname`.out - prints out the received features,
55 and sends back to the compiler an advice (which it gets from `make_response`).
56 It's used for testing, and also to showcase how to set up communication in an
57 interactive ML ("gym") environment.
59 Returns the compiler exit code.
61 to_compiler = temp_rootname +
".in"
62 from_compiler = temp_rootname +
".out"
64 os.mkfifo(to_compiler, 0o666)
65 os.mkfifo(from_compiler, 0o666)
66 compiler_proc = subprocess.Popen(
67 process_and_args, stderr=subprocess.PIPE, stdout=subprocess.DEVNULL
69 with io.BufferedWriter(io.FileIO(to_compiler,
"wb"))
as tc:
70 with io.BufferedReader(io.FileIO(from_compiler,
"rb"))
as fc:
73 while compiler_proc.poll()
is None:
74 next_event = fc.readline()
83 context, next_event, fc, tensor_specs,
None
85 if last_context != context:
86 print(f
"context: {last_context}")
87 context = last_context
88 print(f
"observation: {observation_id}")
92 tensor_values.append(fv)
93 send(tc, make_response(tensor_values), advice_spec)
94 _, err = compiler_proc.communicate()
95 print(err.decode(
"utf-8"))
96 return compiler_proc.wait()
99 os.unlink(to_compiler)
100 os.unlink(from_compiler)