|
1 | 1 | from typing import Optional, Union
|
2 | 2 |
|
| 3 | +import numpy as np |
3 | 4 | import tensorrt as trt
|
4 | 5 | import torch
|
5 | 6 | import torch_tensorrt.dynamo.conversion.impl as impl
|
|
9 | 10 | from torch_tensorrt.dynamo.conversion.converter_utils import (
|
10 | 11 | cast_int_int_div_trt_tensor,
|
11 | 12 | cast_int_or_float_to_bool,
|
| 13 | + cast_trt_tensor, |
12 | 14 | get_trt_tensor,
|
13 | 15 | )
|
14 | 16 | from torch_tensorrt.dynamo.conversion.impl.elementwise.base import (
|
15 | 17 | convert_binary_elementwise,
|
16 | 18 | )
|
17 |
| -from torch_tensorrt.dynamo.conversion.impl.unary import sign |
| 19 | +from torch_tensorrt.dynamo.conversion.impl.unary import atan, sign |
18 | 20 | from torch_tensorrt.dynamo.conversion.impl.unary.base import convert_unary
|
| 21 | +from torch_tensorrt.fx.converters.converter_utils import broadcast |
19 | 22 | from torch_tensorrt.fx.types import TRTTensor
|
20 | 23 | from torch_tensorrt.fx.utils import Frameworks, unified_dtype_converter
|
21 | 24 |
|
@@ -213,6 +216,180 @@ def remainder(
|
213 | 216 | return fmod2_value
|
214 | 217 |
|
215 | 218 |
|
| 219 | +def atan2( |
| 220 | + ctx: ConversionContext, |
| 221 | + target: Target, |
| 222 | + source_ir: Optional[SourceIR], |
| 223 | + name: str, |
| 224 | + input: TRTTensor, |
| 225 | + other: TRTTensor, |
| 226 | +) -> TRTTensor: |
| 227 | + """ |
| 228 | + Perform atan2 operation on Tensor, calculating the arctangent of the quotient of input tensors. |
| 229 | + atan2(x,y) = atan(x/y) if y > 0, |
| 230 | + = atan(x/y) + π if x ≥ 0 and y < 0, |
| 231 | + = atan(x/y) - π if x < 0 and y < 0, |
| 232 | + = π/2 if x > 0 and y = 0, |
| 233 | + = -π/2 if x < 0 and y = 0, |
| 234 | + = 0 if x = 0 and y = 0 |
| 235 | +
|
| 236 | + Args: |
| 237 | + ctx: ConversionContext. |
| 238 | + target: node target |
| 239 | + source_ir (SourceIR): Source IR calling the function. |
| 240 | + name: namespace for the op |
| 241 | + input: Tensor or constant representing the dividend. |
| 242 | + other: Tensor or constant representing the divisor. |
| 243 | +
|
| 244 | + Returns: |
| 245 | + A TensorRT tensor representing the result of the atan2 operation. |
| 246 | + """ |
| 247 | + pi_value = 3.141592653589793 |
| 248 | + pi_tensor = get_trt_tensor(ctx, pi_value, f"{name}_pi") |
| 249 | + |
| 250 | + if isinstance(input, TRTTensor): |
| 251 | + input = cast_trt_tensor(ctx, input, trt.float32, f"{name}_input") |
| 252 | + if isinstance(other, TRTTensor): |
| 253 | + other = cast_trt_tensor(ctx, other, trt.float32, f"{name}_other") |
| 254 | + |
| 255 | + input, other = broadcast(ctx.net, input, other, f"{name}_input", f"{name}_other") |
| 256 | + |
| 257 | + # Calculate x_zero, y_zero (whether inputs are zero) |
| 258 | + x_zero = eq(ctx, target, source_ir, f"{name}_x_zero", input, 0) |
| 259 | + y_zero = eq(ctx, target, source_ir, f"{name}_y_zero", other, 0) |
| 260 | + |
| 261 | + # Get sign of inputs |
| 262 | + x_positive = gt(ctx, target, source_ir, f"{name}_x_positive", input, 0) |
| 263 | + x_zero_positive = ge(ctx, target, source_ir, f"{name}_x_zero_positive", input, 0) |
| 264 | + x_negative = lt(ctx, target, source_ir, f"{name}_x_negative", input, 0) |
| 265 | + y_positive = gt(ctx, target, source_ir, f"{name}_y_positive", other, 0) |
| 266 | + y_negative = lt(ctx, target, source_ir, f"{name}_y_negative", other, 0) |
| 267 | + |
| 268 | + # Calculate atan(x/y) |
| 269 | + input_div_other = div( |
| 270 | + ctx, target, source_ir, f"{name}_input_div_other", input, other |
| 271 | + ) |
| 272 | + atan_val = atan(ctx, target, source_ir, f"{name}_atan", input_div_other) |
| 273 | + |
| 274 | + # atan(x/y)+π if x≥0 and y<0, |
| 275 | + atan_add_pi = add( |
| 276 | + ctx, target, source_ir, f"{name}_atan_add_pi", atan_val, pi_tensor |
| 277 | + ) |
| 278 | + |
| 279 | + # atan(x/y)-π if x<0 and y<0, |
| 280 | + atan_sub_pi = sub( |
| 281 | + ctx, target, source_ir, f"{name}_atan_sub_pi", atan_val, pi_tensor |
| 282 | + ) |
| 283 | + |
| 284 | + # atan(x/y)+π if x≥0 and y<0, |
| 285 | + atan_corrected = impl.condition.select( |
| 286 | + ctx, |
| 287 | + target, |
| 288 | + source_ir, |
| 289 | + f"{name}_atan_corrected", |
| 290 | + atan_add_pi, |
| 291 | + atan_val, |
| 292 | + logical_and( |
| 293 | + ctx, |
| 294 | + target, |
| 295 | + source_ir, |
| 296 | + f"{name}_x_zero_positive_and_y_negative", |
| 297 | + x_zero_positive, |
| 298 | + y_negative, |
| 299 | + ), |
| 300 | + ) |
| 301 | + |
| 302 | + # atan(x/y)-π if x<0 and y<0, |
| 303 | + atan_corrected_2 = impl.condition.select( |
| 304 | + ctx, |
| 305 | + target, |
| 306 | + source_ir, |
| 307 | + f"{name}_atan_corrected_2", |
| 308 | + atan_sub_pi, |
| 309 | + atan_corrected, |
| 310 | + logical_and( |
| 311 | + ctx, |
| 312 | + target, |
| 313 | + source_ir, |
| 314 | + f"{name}_x_negative_and_y_negative", |
| 315 | + x_negative, |
| 316 | + y_negative, |
| 317 | + ), |
| 318 | + ) |
| 319 | + |
| 320 | + # atan(x/y) if y>0 |
| 321 | + atan_output = impl.condition.select( |
| 322 | + ctx, |
| 323 | + target, |
| 324 | + source_ir, |
| 325 | + f"{name}_atan_output", |
| 326 | + atan_val, |
| 327 | + atan_corrected_2, |
| 328 | + y_positive, |
| 329 | + ) |
| 330 | + |
| 331 | + # on x or y-axis |
| 332 | + pi_over_2_tensor = get_trt_tensor( |
| 333 | + ctx, |
| 334 | + (pi_value / 2) * np.ones(input.shape, dtype=np.float32), |
| 335 | + f"{name}_pi_over_2_tensor", |
| 336 | + dtype=trt.float32, |
| 337 | + ) |
| 338 | + minus_pi_over_2_tensor = get_trt_tensor( |
| 339 | + ctx, |
| 340 | + (-pi_value / 2) * np.ones(input.shape, dtype=np.float32), |
| 341 | + f"{name}_minus_pi_over_2_tensor", |
| 342 | + dtype=trt.float32, |
| 343 | + ) |
| 344 | + zero_tensor = get_trt_tensor( |
| 345 | + ctx, |
| 346 | + np.zeros(input.shape, dtype=np.float32), |
| 347 | + f"{name}_zero_tensor", |
| 348 | + dtype=trt.float32, |
| 349 | + ) |
| 350 | + |
| 351 | + # π/2 if x>0 and y=0, |
| 352 | + pi_over_2_output = impl.condition.select( |
| 353 | + ctx, |
| 354 | + target, |
| 355 | + source_ir, |
| 356 | + f"{name}_pi_over_2_output", |
| 357 | + pi_over_2_tensor, |
| 358 | + atan_output, |
| 359 | + logical_and( |
| 360 | + ctx, target, source_ir, f"{name}_x_zero_and_y_positive", x_positive, y_zero |
| 361 | + ), |
| 362 | + ) |
| 363 | + |
| 364 | + # -π/2 if x<0 and y=0, |
| 365 | + minus_pi_over_2_output = impl.condition.select( |
| 366 | + ctx, |
| 367 | + target, |
| 368 | + source_ir, |
| 369 | + f"{name}_minus_pi_over_2_output", |
| 370 | + minus_pi_over_2_tensor, |
| 371 | + pi_over_2_output, |
| 372 | + logical_and( |
| 373 | + ctx, target, source_ir, f"{name}_x_zero_and_y_negative", x_negative, y_zero |
| 374 | + ), |
| 375 | + ) |
| 376 | + |
| 377 | + # 0 if x=0 and y=0, |
| 378 | + zero_output = impl.condition.select( |
| 379 | + ctx, |
| 380 | + target, |
| 381 | + source_ir, |
| 382 | + f"{name}_zero_output", |
| 383 | + zero_tensor, |
| 384 | + minus_pi_over_2_output, |
| 385 | + logical_and( |
| 386 | + ctx, target, source_ir, f"{name}_x_zero_and_y_zero", y_zero, x_zero |
| 387 | + ), |
| 388 | + ) |
| 389 | + |
| 390 | + return zero_output |
| 391 | + |
| 392 | + |
216 | 393 | def clamp(
|
217 | 394 | ctx: ConversionContext,
|
218 | 395 | target: Target,
|
|
0 commit comments