Skip to content

Commit cb12a86

Browse files
generatedunixname89002005307016meta-codesync[bot]
authored andcommitted
Enable Pyrefly in fbcode/pearl
Summary: Automated migration to enable Pyrefly type checking for `fbcode/pearl`. - Added `python.set_pyrefly(True)` to PACKAGE file - Suppressed pre-existing type errors Pyrefly is Meta's next-generation Python type checker, replacing Pyre. If you encounter issues, you can revert the PACKAGE change by removing the `python.set_pyrefly(True)` line. #pyreupgrade Differential Revision: D102231580 fbshipit-source-id: c7970bedd0c10f341cb1826cf7d74fa863db706c
1 parent a4a19b5 commit cb12a86

53 files changed

Lines changed: 110 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pearl/history_summarization_modules/lstm_history_summarization_module.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def summarize_history(
6060
observation.clone().detach().float().view((1, self.observation_dim))
6161
)
6262
if action is None:
63+
# pyrefly: ignore [bad-assignment]
6364
action = self.default_action
6465
assert isinstance(action, torch.Tensor)
6566
action = action.clone().detach().float().view((1, self.action_dim))
@@ -130,6 +131,7 @@ def compare(self, other: HistorySummarizationModule) -> str:
130131
differences.append(
131132
f"action_dim is different: {self.action_dim} vs {other.action_dim}"
132133
)
134+
# pyrefly: ignore [bad-argument-type]
133135
if not torch.allclose(self.default_action, other.default_action):
134136
differences.append(
135137
f"default_action is different: {self.default_action} vs {other.default_action}"

pearl/history_summarization_modules/stacking_history_summarization_module.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def summarize_history(
4040
self, observation: Observation, action: Action | None
4141
) -> torch.Tensor:
4242
if action is None:
43+
# pyrefly: ignore [bad-assignment]
4344
action = self.default_action
4445

4546
observation = assert_is_tensor_like(observation)
@@ -103,6 +104,7 @@ def compare(self, other: HistorySummarizationModule) -> str:
103104
differences.append(
104105
f"action_dim is different: {self.action_dim} vs {other.action_dim}"
105106
)
107+
# pyrefly: ignore [bad-argument-type]
106108
if not torch.allclose(self.default_action, other.default_action):
107109
differences.append(
108110
f"default_action is different: {self.default_action} vs {other.default_action}"

pearl/history_summarization_modules/transformer_history_summarization_module.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
3939
x: [B, T, d_model]
4040
"""
4141
T = x.size(1)
42+
# pyrefly: ignore [bad-index]
4243
x = x + self.pe[:T, :].unsqueeze(0)
4344
return self.dropout(x)
4445

@@ -137,6 +138,7 @@ def __init__(
137138
d_model=d_model, dropout=dropout
138139
)
139140
elif pos_encoding == "sinusoidal":
141+
# pyrefly: ignore [bad-assignment]
140142
self.pos_encoding = SinusoidalPositionalEncoding(
141143
d_model=d_model, dropout=dropout
142144
)
@@ -206,6 +208,7 @@ def summarize_history(
206208
act = action.clone().detach().float().view(1, self.action_dim)
207209

208210
# Concatenate (action, observation) for current step
211+
# pyrefly: ignore [no-matching-overload]
209212
pair = torch.cat((act, obs), dim=-1) # [1, input_dim]
210213
assert pair.shape[-1] == self.history.shape[-1]
211214

@@ -279,6 +282,7 @@ def compare(self, other: HistorySummarizationModule) -> str:
279282
)
280283

281284
# Buffers
285+
# pyrefly: ignore [bad-argument-type]
282286
if not torch.allclose(self.default_action, other.default_action):
283287
differences.append(
284288
f"default_action is different: {self.default_action} vs {other.default_action}"
@@ -288,6 +292,7 @@ def compare(self, other: HistorySummarizationModule) -> str:
288292

289293
# Positional encoding (buffer + dropout p)
290294
if hasattr(self.pos_encoding, "pe") and hasattr(other.pos_encoding, "pe"):
295+
# pyrefly: ignore [bad-argument-type]
291296
if not torch.allclose(self.pos_encoding.pe, other.pos_encoding.pe):
292297
differences.append("positional_encoding.pe is different")
293298
p_self = getattr(self.pos_encoding.dropout, "p", None)

pearl/neural_networks/common/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
"ValueNetwork",
1919
"CNNValueNetwork",
2020
"VanillaValueNetwork",
21+
# pyrefly: ignore [bad-dunder-all]
2122
"Epinet",
2223
]

pearl/neural_networks/common/epistemic_neural_networks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def __init__(
165165
self.models: nn.ModuleList = nn.ModuleList(models)
166166

167167
self.params: dict[str, Any]
168+
# pyrefly: ignore [bad-override]
168169
self.buffers: dict[str, Any]
169170
self.generate_params_buffers()
170171

pearl/neural_networks/common/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,15 @@ def mlp_block(
111111
nn.Linear(input_dim_current_layer, output_dim_current_layer)
112112
)
113113
if use_layer_norm:
114+
# pyrefly: ignore [bad-argument-type]
114115
single_layers.append(nn.LayerNorm(output_dim_current_layer))
115116
if dropout_ratio > 0:
117+
# pyrefly: ignore [bad-argument-type]
116118
single_layers.append(nn.Dropout(p=dropout_ratio))
119+
# pyrefly: ignore [bad-argument-type]
117120
single_layers.append(ActivationType(hidden_activation).module())
118121
if use_batch_norm:
122+
# pyrefly: ignore [bad-argument-type]
119123
single_layers.append(nn.BatchNorm1d(output_dim_current_layer))
120124
single_layer_model = nn.Sequential(*single_layers)
121125
if use_skip_connections:
@@ -133,6 +137,7 @@ def mlp_block(
133137
last_layer = []
134138
last_layer.append(nn.Linear(dims[-2], dims[-1]))
135139
if last_activation is not None:
140+
# pyrefly: ignore [bad-argument-type]
136141
last_layer.append(ActivationType(last_activation).module())
137142
last_layer_model = nn.Sequential(*last_layer)
138143
if use_skip_connections:
@@ -184,7 +189,9 @@ def conv_block(
184189
layers.append(conv_layer)
185190
if use_batch_norm:
186191
# batch norm should normalize the output of the convolutional layer
192+
# pyrefly: ignore [bad-argument-type]
187193
layers.append(nn.BatchNorm2d(out_channels))
194+
# pyrefly: ignore [bad-argument-type]
188195
layers.append(nn.ReLU())
189196
# number of input channels to next layer is number of output channels of previous layer:
190197
input_channels_count = out_channels

pearl/neural_networks/contextual_bandit/linear_regression.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def __init__(
9090
@property
9191
def A(self) -> torch.Tensor:
9292
# return A with L2 regularization applied
93+
# pyrefly: ignore [no-matching-overload]
9394
return self._A + self.l2_reg_lambda * torch.eye(
9495
self._feature_dim + 1, device=self._A.device
9596
)
@@ -209,8 +210,11 @@ def learn_batch(
209210
torch.distributed.all_reduce(delta_b)
210211
torch.distributed.all_reduce(delta_sum_weight)
211212

213+
# pyrefly: ignore [no-matching-overload]
212214
self._A += delta_A.to(self._A.device)
215+
# pyrefly: ignore [no-matching-overload]
213216
self._b += delta_b.to(self._b.device)
217+
# pyrefly: ignore [no-matching-overload]
214218
self._sum_weight += delta_sum_weight.to(self._sum_weight.device)
215219

216220
self.calculate_coefs() # update coefs after updating A and b
@@ -226,7 +230,9 @@ def apply_discounting(self) -> None:
226230
"""
227231
if self.gamma < 1:
228232
logger.info(f"Applying discounting at sum_weight={self._sum_weight}")
233+
# pyrefly: ignore [bad-argument-type, unsupported-operation]
229234
self._A *= self.gamma
235+
# pyrefly: ignore [bad-argument-type, unsupported-operation]
230236
self._b *= self.gamma
231237
# don't dicount sum_weight because it's used to determine when to apply discounting
232238

@@ -250,6 +256,7 @@ def calculate_coefs(self) -> None:
250256
Save inverted A and coefficients in buffers.
251257
"""
252258
self._inv_A = self.matrix_inv_fallback_pinv(self.A)
259+
# pyrefly: ignore [bad-argument-type]
253260
self._coefs = torch.matmul(self._inv_A, self._b)
254261

255262
def calculate_sigma(self, x: torch.Tensor) -> torch.Tensor:
@@ -300,10 +307,13 @@ def compare(self, other: MuSigmaCBModel) -> str:
300307
f"distribution_enabled is different: {self.distribution_enabled} "
301308
+ f"vs {other.distribution_enabled}"
302309
)
310+
# pyrefly: ignore [bad-argument-type]
303311
if not torch.allclose(self._A, other._A):
304312
differences.append(f"_A is different: {self._A} vs {other._A}")
313+
# pyrefly: ignore [bad-argument-type]
305314
if not torch.allclose(self._b, other._b):
306315
differences.append(f"_b is different: {self._b} vs {other._b}")
316+
# pyrefly: ignore [bad-argument-type]
307317
if not torch.allclose(self._sum_weight, other._sum_weight):
308318
differences.append(
309319
f"_sum_weight is different: {self._sum_weight} vs {other._sum_weight}"

pearl/neural_networks/sequential_decision_making/actor_networks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ def sample_action(
577577

578578
log_prob = normal.log_prob(sample)
579579
log_prob -= torch.log(
580+
# pyrefly: ignore [unsupported-operation]
580581
self._action_bound * (1 - normalized_action.pow(2)) + epsilon
581582
)
582583

@@ -617,6 +618,7 @@ def get_log_probability(
617618
unnormalized_action_batch = torch.atanh(normalized_action_batch)
618619
log_prob = normal.log_prob(unnormalized_action_batch)
619620
log_prob -= torch.log(
621+
# pyrefly: ignore [unsupported-operation]
620622
self._action_bound * (1 - normalized_action_batch.pow(2)) + epsilon
621623
)
622624

pearl/neural_networks/sequential_decision_making/q_value_networks.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ def __init__(
294294
)
295295
self.register_buffer(
296296
"_quantile_midpoints",
297+
# pyrefly: ignore [bad-index]
297298
((self._quantiles[1:] + self._quantiles[:-1]) / 2)
298299
.unsqueeze(0)
299300
.unsqueeze(0),
@@ -328,10 +329,12 @@ def get_q_value_distribution(
328329

329330
@property
330331
def quantiles(self) -> Tensor:
332+
# pyrefly: ignore [bad-return]
331333
return self._quantiles
332334

333335
@property
334336
def quantile_midpoints(self) -> Tensor:
337+
# pyrefly: ignore [bad-return]
335338
return self._quantile_midpoints
336339

337340
@property

pearl/policy_learners/contextual_bandits/contextual_bandit_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def learn_batch(self, batch: TransitionBatch) -> dict[str, Any]:
6060
pass
6161

6262
@abstractmethod
63+
# pyrefly: ignore [bad-override]
6364
def act(
6465
self,
6566
subjective_state: SubjectiveState,

0 commit comments

Comments
 (0)