René's URL Explorer Experiment


Title: Disparate Performance between Python and Java · Issue #602 · tensorflow/java · GitHub

Open Graph Title: Disparate Performance between Python and Java · Issue #602 · tensorflow/java

X Title: Disparate Performance between Python and Java · Issue #602 · tensorflow/java

Description: Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template System information Have I written custom code (a...

Open Graph Description: Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template System i...

X Description: Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template System i...

Opengraph URL: https://github.com/tensorflow/java/issues/602

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Disparate Performance between Python and Java","articleBody":"\u003cem\u003ePlease make sure that this is a bug. As per our [GitHub Policy](https://github.com/tensorflow/tensorflow/blob/master/ISSUES.md), we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template\u003c/em\u003e\n\n**System information**\n- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes\n- OS Platform and Distribution (e.g., Linux Ubuntu 16.04 x86\\_64): Ubuntu 24.04 x86\\_64\n- TensorFlow installed from (source or binary): binary\n- TensorFlow version (use command below): 1.0.0 \n- Java version (i.e., the output of `java -version`): openjdk version \"21.0.6\" 2025-01-21\n- Java command line flags (e.g., GC parameters): \n- Python version (if transferring a model trained in Python): 3.12.8\n- Bazel version (if compiling from source):\n- GCC/Compiler version (if compiling from source):\n- CUDA/cuDNN version: 12.8.61/8905\n- GPU model and memory: V100 (32GB)\n\n**Describe the current behavior**\n\nExecuting the exported model using Tensorflow in Python takes significantly less time than when calling the same function from using Tensorflow Java. I suspect that I am just not using the Java API correctly, because a small change to the python can lead to comparably poor performance in the python.\n\n**Describe the expected behavior**\n\nThe function calls should take a comparable amount of time.\n\n**Code to reproduce the issue**\n\nI have the following python function:\n\n```python\n@tf.function(\n    input_signature=[\n            tf.TensorSpec(shape=[41, 2048, 2048], dtype=tf.float32, name=\"data\"),  # [k, n, n]\n            tf.TensorSpec(shape=[1, 2048, 2048], dtype=tf.float32, name=\"image\"),  # [1, n, n]\n            tf.TensorSpec(shape=[41, 2048, 2048], dtype=tf.float32, name=\"psf\"),  # [k, n, n]\n    ],\n    jit_compile=True\n)\ndef rl_step(\n    data: tf.Tensor,  # [k, n, n]\n    image: tf.Tensor, # [1, n, n]\n    psf: tf.Tensor,   # [k, n, n]\n) -\u003e tf.Tensor: # [k, n, n]\n    psf_fft = tf.signal.rfft2d(psf)\n    psft_fft = tf.signal.rfft2d(tf.reverse(psf, axis=(-2, -1)))\n    denom = tf.reduce_sum(\n        tf.signal.irfft2d(psf_fft * tf.signal.rfft2d(data)),\n        axis=0,\n        keepdims=True\n    )\n    img_err = image / denom\n    return data * tf.signal.irfft2d(tf.signal.rfft2d(img_err) * psft_fft)\n```\n\nIn python, this function is applied iteratively over the same tensor as below:\n\n```python\n    image_tensor = tf.constant(image) # [k, n, n]\n    measured_psf_tensor = tf.constant(measured_psf) # [1, n, n]\n    data_tensor = tf.constant(data) # [k, n, n]\n\n    for i in range(10):\n        start = time()\n        data = rl_step(data_tensor, image_tensor, measured_psf_tensor)\n        print(f\"Iter {i}:\", time() - start, \"seconds.\")\n```\nHere `image`, `measured_psf`, and `data` are all 3D arrays with dtype=float32 and `n=2048` and `k=41`\n\nThis prints timings around the following:\n\n```bash\nIter 0: 0.2061774730682373 seconds.\nIter 1: 0.004193544387817383 seconds.\nIter 2: 0.0007469654083251953 seconds.\nIter 3: 0.000415802001953125 seconds.\nIter 4: 0.0004220008850097656 seconds.\nIter 5: 0.0004246234893798828 seconds.\nIter 6: 0.0004112720489501953 seconds.\nIter 7: 0.00042128562927246094 seconds.\nIter 8: 0.0004055500030517578 seconds.\nIter 9: 0.00040721893310546875 seconds.\n```\n\nI tried exporting the model by adding the following after the timing code:\n\n```python\n    mod = tf.Module()\n    mod.f = rl_step\n    tf.saved_model.save(mod, \"pure_tf_export\")\n```\n\nNow I tried to use this exported mode from the Java API,\n\n\n```java\n        String modelLocation = \"./pure_tf_export\";\n        try(Graph g = new Graph(); Session s = new Session(g)){\n            SavedModelBundle model = SavedModelBundle.loader(modelLocation).load();\n\n            try (Tensor imageTensor = TFloat32.tensorOf(image);\n                Tensor psfTensor = TFloat32.tensorOf(psf);\n                Tensor dataTensor = TFloat32.tensorOf(data)\n            ){\n                Map\u003cString, Tensor\u003e inputs = new HashMap\u003cString, Tensor\u003e();\n                inputs.put(\"data\", dataTensor);\n                inputs.put(\"image\", imageTensor);\n                inputs.put(\"psf\", psfTensor);\n\n                for (int i = 0; i \u003c 10; i++){\n\n                    Instant start = Instant.now();\n\n                    Result result = model.function(\"serving_default\").call(inputs);\n                    inputs.replace(\"data\", result.get(\"output_0\").get());\n\n                    System.out.println(\"Iter \" + i + \" \" + (Duration.between(start, Instant.now()).toMillis()/1000f) + \" seconds\");\n                }\n            }\n        }\n```\nAnd I get timings as follows:\n\n```bash\nIter 0 0.701 seconds\nIter 1 0.528 seconds\nIter 2 0.874 seconds\nIter 3 0.224 seconds\nIter 4 0.254 seconds\nIter 5 1.622 seconds\nIter 6 0.241 seconds\nIter 7 0.224 seconds\nIter 8 0.231 seconds\nIter 9 0.228 seconds\n```\n\nI am pretty sure I am making a simple mistake somewhere. I suspect it is in how I am instantiating the Tensors. I know in python if you don't use `tf.constant` the timings go up a lot.\n\nAny help would be very much appreciated. I tried looking through the documentation and the tensorflow java-examples repository, but couldn't spot what I am doing wrong.\n\nThanks again!","author":{"url":"https://github.com/ryanhausen","@type":"Person","name":"ryanhausen"},"datePublished":"2025-02-11T22:12:09.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/602/java/issues/602"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:1455abad-afdf-3c52-d668-38028eeabe97
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9D86:296AF5:217CCF:2EC670:696A6567
html-safe-nonce7815b459b763eddf469c62a068898aec7d9a4382b5d0d6d95c507fdc3cc8707e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDg2OjI5NkFGNToyMTdDQ0Y6MkVDNjcwOjY5NkE2NTY3IiwidmlzaXRvcl9pZCI6IjQ0NjQxNjIzMjQ1OTkxNzA0MDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac30a51b110a607af9b50779582a0626f8dfc81a636219945e880d218cae6c2709
hovercard-subject-tagissue:2846652863
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/tensorflow/java/602/issue_layout
twitter:imagehttps://opengraph.githubassets.com/bec87f1a0870a144941e9677a0c9046f19fcfd52ebf83fa1608d03d4abdb5479/tensorflow/java/issues/602
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/bec87f1a0870a144941e9677a0c9046f19fcfd52ebf83fa1608d03d4abdb5479/tensorflow/java/issues/602
og:image:altPlease make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template System i...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameryanhausen
hostnamegithub.com
expected-hostnamegithub.com
None9b7735a184970dd9333b2cbe036c8f3c0a9108c64aaa93827c5a64fc70993392
turbo-cache-controlno-preview
go-importgithub.com/tensorflow/java git https://github.com/tensorflow/java.git
octolytics-dimension-user_id15658638
octolytics-dimension-user_logintensorflow
octolytics-dimension-repository_id207384523
octolytics-dimension-repository_nwotensorflow/java
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id207384523
octolytics-dimension-repository_network_root_nwotensorflow/java
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release87cbd411c2982752221b5751d583a515b23bf5fa
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/tensorflow/java/issues/602#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Ftensorflow%2Fjava%2Fissues%2F602
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Ftensorflow%2Fjava%2Fissues%2F602
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=tensorflow%2Fjava
Reloadhttps://github.com/tensorflow/java/issues/602
Reloadhttps://github.com/tensorflow/java/issues/602
Reloadhttps://github.com/tensorflow/java/issues/602
tensorflow https://github.com/tensorflow
javahttps://github.com/tensorflow/java
Notifications https://github.com/login?return_to=%2Ftensorflow%2Fjava
Fork 223 https://github.com/login?return_to=%2Ftensorflow%2Fjava
Star 913 https://github.com/login?return_to=%2Ftensorflow%2Fjava
Code https://github.com/tensorflow/java
Issues 151 https://github.com/tensorflow/java/issues
Pull requests 23 https://github.com/tensorflow/java/pulls
Actions https://github.com/tensorflow/java/actions
Projects 0 https://github.com/tensorflow/java/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/tensorflow/java/security
Please reload this pagehttps://github.com/tensorflow/java/issues/602
Insights https://github.com/tensorflow/java/pulse
Code https://github.com/tensorflow/java
Issues https://github.com/tensorflow/java/issues
Pull requests https://github.com/tensorflow/java/pulls
Actions https://github.com/tensorflow/java/actions
Projects https://github.com/tensorflow/java/projects
Security https://github.com/tensorflow/java/security
Insights https://github.com/tensorflow/java/pulse
New issuehttps://github.com/login?return_to=https://github.com/tensorflow/java/issues/602
New issuehttps://github.com/login?return_to=https://github.com/tensorflow/java/issues/602
Disparate Performance between Python and Javahttps://github.com/tensorflow/java/issues/602#top
https://github.com/ryanhausen
https://github.com/ryanhausen
ryanhausenhttps://github.com/ryanhausen
on Feb 11, 2025https://github.com/tensorflow/java/issues/602#issue-2846652863
GitHub Policyhttps://github.com/tensorflow/tensorflow/blob/master/ISSUES.md
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.