Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] javacpp -pytorch UpsampleImpl have bad bug ,can not work #1592

Open
mullerhai opened this issue Mar 7, 2025 · 8 comments
Open

[bug] javacpp -pytorch UpsampleImpl have bad bug ,can not work #1592

mullerhai opened this issue Mar 7, 2025 · 8 comments

Comments

@mullerhai
Copy link

Hi, @saudet
UpsampleImpl javacpp have bad bug, the same input parameter cannot work in javacpp ,please fix up

in python example
···
input_3x3 = torch.zeros(3, 3).view(1, 1, 3, 3)
print(input_3x3.shape)
m = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
# Notice that values in top left corner are now changed
output = m(input_3x3)
print(output.shape)

···

python console

input_3x3  torch.Size([1, 1, 3, 3])
output size torch.Size([1, 1, 6, 6])

in javacpp


    val input3 = torch.zeros(Seq(3, 3)).view(1, 1, 3, 3)
    val options: UpsampleOptions = UpsampleOptions()
    options.scale_factor().put(DoubleVector(2))
    options.align_corners().put(true)
    options.mode().put(new kBilinear())
//    options.size().put(LongPointer(3,3))
    val model = UpsampleImpl(options)

    println(s"  align ${model.options().align_corners().get()}  scala factor ${model.options().scale_factor().get().get(0)} mode ${model.options().mode()}")
    val output = fromNative(model.forward(input3.native))
    println(s"output ${output.shape}")

console log

  align true  scala factor 0.0 mode org.bytedeco.pytorch.UpsampleMode[address=0x25be4257950,position=0,limit=0,capacity=0,deallocator=null]

java.lang.RuntimeException: Input and output sizes should be greater than 0, but got input (H: 3, W: 3) output (H: 0, W: 0)
Exception raised from upsample_2d_common_check at D:\a\javacpp-presets\javacpp-presets\pytorch\cppbuild\windows-x86_64-gpu\pytorch\aten\src\ATen/native/UpSample.h:165 (most recent call first):
00007FF8894F83C9 <unknown symbol address> c10.dll!<unknown symbol> [<unknown file> @ <unknown line number>]
00007FF8894F6C5A <unknown symbol address> c10.dll!<unknown symbol> [<unknown file> @ <unknown line number>]

@saudet
Copy link
Member

saudet commented Mar 8, 2025

You'll need to set the values of scale_factor for this to work

@saudet saudet removed the bug label Mar 8, 2025
@mullerhai
Copy link
Author

scale_factor
Hi, @saudet
I promise I have set the scale_factor field , options.scale_factor().put(DoubleVector(2)), does you not found it? , I think if we want to fixup the bug maybe could run my test code ,to find really bug or give me the really code content ,thanks

@saudet
Copy link
Member

saudet commented Mar 8, 2025

Do like #1594

@mullerhai
Copy link
Author

Do like #1594

for DoubleVector put value maybe is complex than DoublePointer

I try to use options.scale_factor().put(DoubleVector(1).put(3)), then scale_factor is set ,but forward() meet error
also tell me error [java.lang.RuntimeException: invalid vector subscript]

Testing started at 17:24 ...
  align true  scala factor 3.0   mode org.bytedeco.pytorch.UpsampleMode[address=0x1cb5a739640,position=0,limit=0,capacity=0,deallocator=null]

java.lang.RuntimeException: invalid vector subscript

@mullerhai
Copy link
Author

HI ,@saudet pure javacpp code, unsample really can not work

   val input = torch.arange(1, 5, dtype = torch.float32).view(1, 1, 2, 2)
    val options: UpsampleOptions = UpsampleOptions()
    options.scale_factor().put(DoubleVector(1).put(2))
    options.mode().put(new kNearest())
    val model = UpsampleImpl(options)
    val output = fromNative(model.forward(input.native))
    println(s"output ${output.shape}")

console log

input: tensor dtype=float32, shape=[1, 1, 2, 2], device=CPU 
[[[[1.0000, 2.0000],
   [3.0000, 4.0000]]]]
java.lang.RuntimeException: invalid vector subscript

	at org.bytedeco.pytorch.UpsampleImpl.forward(Native Method)
	at torch.nn.modules.UpsampleRawSuite.$init$$$anonfun$10(otherSuite.scala:187)



same python code

>>> input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
>>> input
tensor([[[[1., 2.],
          [3., 4.]]]])

>>> m = nn.Upsample(scale_factor=2, mode='nearest')
>>> m(input)

tensor([[[[1., 1., 2., 2.],
          [1., 1., 2., 2.],
          [3., 3., 4., 4.],
          [3., 3., 4., 4.]]]])

@saudet
Copy link
Member

saudet commented Mar 9, 2025

Please try to set the "org.bytedeco.javacpp.nopointergc" system property to "true".

@mullerhai
Copy link
Author

Please try to set the "org.bytedeco.javacpp.nopointergc" system property to "true".

·```
class AdapativeMaxPool2dRawSuite3 extends munit.FunSuite {
test("AdapativeMaxPool2d output shapes") {

  System.setProperty("org.bytedeco.javacpp.nopointergc", "true")
  val nativeOutputSize = LongPointer(Array(5l, 7l) *)
  val options: AdaptiveMaxPool2dOptions = AdaptiveMaxPool2dOptions(nativeOutputSize)
  val nativeModule: AdaptiveMaxPool2dImpl = AdaptiveMaxPool2dImpl(options)
  println(s"AdaptiveMaxPool2d raw options 1: ${options.output_size().get} options 2: ${options.output_size().get} ")
  val pi = options.output_size()
  println(s"pi : ${pi}")
  val input = torch.randn(Seq(1, 64, 8, 9))
  val output = fromNative(nativeModule.forward(input.native))
  println(s"AdaptiveMaxPool2d nativeModule options 1: ${nativeModule.options.output_size().get} options 2: ${nativeModule.options.output_size()} ")
  val shape = output.shape
  println(s"output shape: ${output.shape}")
  assertEquals(shape, Seq(1, 64, 5, 7))
}

}


console log

Testing started at 19:43 ...
AdaptiveMaxPool2d raw options 1: 5 options 2: 5
pi : org.bytedeco.pytorch.LongOptional[address=0x2b2d88a8620,position=0,limit=0,capacity=0,deallocator=null]
AdaptiveMaxPool2d nativeModule options 1: 5 options 2: org.bytedeco.pytorch.LongOptional[address=0x2b2d92c1970,position=0,limit=0,capacity=0,deallocator=null]
output shape: ArraySeq(1, 64, 5, 0)

munit.ComparisonFailException: D:\data\storch_demo\src\main\scala\torch\pooltest.scala:422
421: println(s"output shape: ${output.shape}")
422: assertEquals(shape, Seq(1, 64, 5, 7))
423: // val m1 = new AdaptiveAvgPool2d((5, 7))
values are not the same
=> Obtained
ArraySeq(
1,
64,
5,
0
)
=> Diff (- obtained, + expected)
-ArraySeq(
+List(
1,
5,

  • 0
  • 7
    )
    Expected :List(1, 64, 5, 7)
    Actual :ArraySeq(1, 64, 5, 0)

@mullerhai
Copy link
Author

·```
class UpsampleRawSuite extends munit.FunSuite {
test("UpsampleRawSuite output shapes") {
System.setProperty("org.bytedeco.javacpp.nopointergc", "true")
val input = torch.arange(1, 5, dtype = torch.float32).view(1, 1, 2, 2)
val input3 = torch.zeros(Seq(3, 3)).view(1, 1, 3, 3)
val options: UpsampleOptions = UpsampleOptions()
options.scale_factor().put(DoubleVector(1).put(2))
options.mode().put(new kNearest())
println(s"input: ${input}")
val model = UpsampleImpl(options)

val output = fromNative(model.forward(input.native.to(ScalarType.Float)))
println(s"output ${output.shape}")

console log

Testing started at 19:46 ...
input: tensor dtype=float32, shape=[1, 1, 2, 2], device=CPU
[[[[1.0000, 2.0000],
[3.0000, 4.0000]]]]

java.lang.RuntimeException: invalid vector subscript

at org.bytedeco.pytorch.UpsampleImpl.forward(Native Method)
at torch.nn.modules.UpsampleRawSuite.$init$$$anonfun$10(otherSuite.scala:190)

·```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants