facebookresearch / CompilerGym

Reinforcement learning environments for compiler and program optimization tasks
https://compilergym.ai/
MIT License
880 stars 123 forks source link

I couldn't run code "env.fork()" #826

Open Rain0802 opened 5 months ago

Rain0802 commented 5 months ago

When I run below code in the tutorial Chapter “Autotuning”,

# Import the compiler_gym namespace
import compiler_gym

# Create an LLVM CompilerGym environment
env = compiler_gym.make("llvm-v0")

# Set the benchmark. We'll use cBench/susan
env.reset(benchmark = "benchmark://cbench-v1/susan")

# We'll use IrInstructionCount as reward and observation
env.observation_space = "IrInstructionCount"
env.reward_space = "IrInstructionCount"
print(f"Total instructions at start = {env.observation['IrInstructionCount']}")

# Here we do our hill climbing. We'll do one hundred steps.
num_steps = 100

for i in range(num_steps):
  # We will need to back track if the action isn't good for us.
  # In CompilerGym, you can `fork` an environment and kill it later if you 
  # don't like it. That will allow us tohave the same effect as having `back`.
  candidate = env.fork()

  # We will choose a rancdom action from the action space
  action = candidate.action_space.sample()

  # And now apply it to the program
  observation, reward, done, info = candidate.step(action)

  print(f"{i}: Action {action} = {candidate.action_space.names[action]}", end="")
  print(f" gave reward {reward} for total instructions {observation}", end="")
  print(f". Accept = {reward > 0}")

  if reward <= 0:
    # If we don't like it we can throw it away.
    candidate.close()
  else:
    # If we like it, we can replace `env` with it.
    env.close()
    env = candidate

I met such reported errors in the console,

/home/rain/anaconda3/lib/python3.11/site-packages/gym/utils/passive_env_checker.py:195: UserWarning: WARN: The result returned by `env.reset()` was not a tuple of the form `(obs, info)`, where `obs` is a observation and `info` is a dictionary containing additional information. Actual type: `<class 'str'>`
  logger.warn(
Total instructions at start = 13282
Traceback (most recent call last):
  File "/home/rain/PycharmProjects/CompilerGymPractice/main.py", line 61, in <module>
    candidate = env.fork()
                ^^^^^^^^^^
  File "/home/rain/anaconda3/lib/python3.11/site-packages/compiler_gym/envs/compiler_env.py", line 599, in fork
    new_env.reward.spaces = deepcopy(self.reward.spaces)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rain/anaconda3/lib/python3.11/copy.py", line 146, in deepcopy
    y = copier(x, memo)
        ^^^^^^^^^^^^^^^
  File "/home/rain/anaconda3/lib/python3.11/copy.py", line 231, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
                             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/rain/anaconda3/lib/python3.11/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rain/anaconda3/lib/python3.11/copy.py", line 273, in _reconstruct
    y.__setstate__(state)
  File "/home/rain/anaconda3/lib/python3.11/site-packages/gym/spaces/space.py", line 125, in __setstate__
    state = dict(state)
            ^^^^^^^^^^^
TypeError: cannot convert dictionary update sequence element #0 to a sequence
ERROR: Killing a service with 2 active sessions!
Exception ignored in: <function CompilerEnv.__del__ at 0x7f8bebe89a80>
Traceback (most recent call last):
  File "/home/rain/anaconda3/lib/python3.11/site-packages/compiler_gym/envs/compiler_env.py", line 670, in __del__
  File "/home/rain/anaconda3/lib/python3.11/site-packages/compiler_gym/envs/compiler_env.py", line 661, in close
  File "/home/rain/anaconda3/lib/python3.11/site-packages/compiler_gym/service/connection.py", line 681, in close
  File "/home/rain/anaconda3/lib/python3.11/site-packages/compiler_gym/service/connection.py", line 457, in close
compiler_gym.service.connection.ServiceError: Service exited with returncode 6

It seems that something goes wrong with "env.fork()", could anybody tell me why, thank you!