chore: remove torch in main code (#439)

* use np.ndarray in costeer model evaluators

* remove package

* fix test error

---------

Co-authored-by: Xu Yang <peteryang@vip.qq.com>
This commit is contained in:
Linlang
2024-10-21 15:56:04 +08:00
committed by GitHub
parent 671d4f0600
commit 33684cafe4
7 changed files with 14 additions and 71 deletions
@@ -4,7 +4,6 @@ from pathlib import Path
from typing import List, Tuple
import numpy as np
import torch
from jinja2 import Environment, StrictUndefined
from rdagent.components.coder.model_coder.conf import MODEL_IMPL_SETTINGS
@@ -25,7 +24,7 @@ from rdagent.oai.llm_utils import APIBackend
evaluate_prompts = Prompts(file_path=Path(__file__).parent.parent / "prompts.yaml")
def shape_evaluator(prediction: torch.Tensor | np.ndarray, target_shape: Tuple = None) -> Tuple[str, bool]:
def shape_evaluator(prediction: np.ndarray, target_shape: Tuple = None) -> Tuple[str, bool]:
if target_shape is None or prediction is None:
return (
"No output generated from the model. No shape evaluation conducted.",
@@ -42,18 +41,10 @@ def shape_evaluator(prediction: torch.Tensor | np.ndarray, target_shape: Tuple =
)
def reshape_tensor(original_tensor, target_shape):
new_tensor = torch.zeros(target_shape)
for i, dim in enumerate(original_tensor.shape):
new_tensor = new_tensor.narrow(i, 0, dim).copy_(original_tensor)
return new_tensor
def value_evaluator(
prediction: torch.Tensor,
target: torch.Tensor,
) -> Tuple[torch.Tensor, bool]:
prediction: np.ndarray,
target: np.ndarray,
) -> Tuple[np.ndarray, bool]:
if prediction is None:
return "No output generated from the model. Skip value evaluation", False
elif target is None:
@@ -63,7 +54,7 @@ def value_evaluator(
)
else:
# Calculate the mean absolute difference
diff = torch.mean(torch.abs(target - prediction)).item()
diff = np.mean(np.abs(target - prediction))
return (
f"The value of the output is correct. The mean absolute difference is {diff}.",
diff < 0.1,
@@ -273,7 +264,7 @@ class ModelCoderEvaluator(Evaluator):
param_init_value = 0.6
assert isinstance(implementation, ModelFBWorkspace)
model_execution_feedback, gen_tensor = implementation.execute(
model_execution_feedback, gen_np_array = implementation.execute(
batch_size=batch_size,
num_features=num_features,
num_timesteps=num_timesteps,
@@ -282,7 +273,7 @@ class ModelCoderEvaluator(Evaluator):
)
if gt_implementation is not None:
assert isinstance(gt_implementation, ModelFBWorkspace)
_, gt_tensor = gt_implementation.execute(
_, gt_np_array = gt_implementation.execute(
batch_size=batch_size,
num_features=num_features,
num_timesteps=num_timesteps,
@@ -290,10 +281,10 @@ class ModelCoderEvaluator(Evaluator):
param_init_value=param_init_value,
)
else:
gt_tensor = None
gt_np_array = None
shape_feedback, shape_decision = shape_evaluator(gen_tensor, (batch_size, 1))
value_feedback, value_decision = value_evaluator(gen_tensor, gt_tensor)
shape_feedback, shape_decision = shape_evaluator(gen_np_array, (batch_size, 1))
value_feedback, value_decision = value_evaluator(gen_np_array, gt_np_array)
code_feedback, _ = ModelCodeEvaluator(scen=self.scen).evaluate(
target_task=target_task,
implementation=implementation,
@@ -37,7 +37,7 @@ if MODEL_TYPE == "Graph":
else:
out = m(data)
execution_model_output = out.cpu().detach()
execution_model_output = out.cpu().detach().numpy()
execution_feedback_str = f"Execution successful, output tensor shape: {execution_model_output.shape}"
pickle.dump(execution_model_output, open("execution_model_output.pkl", "wb"))
@@ -12,7 +12,7 @@ valid_X = pd.DataFrame(np.random.randn(8, 30), columns=[f"{i}" for i in range(30
valid_y = pd.Series(np.random.randint(0, 2, 8))
model = fit(train_X, train_y, valid_X, valid_y)
execution_model_output = predict(model, valid_X)
execution_model_output = predict(model, valid_X).cpu().detach().numpy()
execution_feedback_str = f"Execution successful, output numpy ndarray shape: {execution_model_output.shape}"