-
Notifications
You must be signed in to change notification settings - Fork 13.8k
nicer autodiff error handling #142842
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
base: master
Are you sure you want to change the base?
nicer autodiff error handling #142842
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ use tracing::debug; | |
use super::link::{self, ensure_removed}; | ||
use super::lto::{self, SerializedModule}; | ||
use super::symbol_export::symbol_name_for_instance_in_crate; | ||
use crate::errors::{AutodiffWithoutLto, ErrorCreatingRemarkDir}; | ||
use crate::errors::{AutodiffLibraryBuild, AutodiffWithoutLto, ErrorCreatingRemarkDir}; | ||
use crate::traits::*; | ||
use crate::{ | ||
CachedModuleCodegen, CodegenResults, CompiledModule, CrateInfo, ModuleCodegen, ModuleKind, | ||
|
@@ -419,7 +419,13 @@ fn generate_lto_work<B: ExtraBackendMethods>( | |
} else { | ||
if !autodiff.is_empty() { | ||
let dcx = cgcx.create_dcx(); | ||
dcx.handle().emit_fatal(AutodiffWithoutLto {}); | ||
let span = autodiff[0].span; | ||
if cgcx.crate_types.contains(&CrateType::Rlib) { | ||
dcx.handle().span_fatal(AutodiffLibraryBuild { span }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to stay `emit_fatal, unsure what span_fatal does |
||
} | ||
if cgcx.lto != Lto::Fat { | ||
dcx.handle().emit_fatal(AutodiffWithoutLto { span }); | ||
} | ||
} | ||
assert!(needs_fat_lto.is_empty()); | ||
let (lto_modules, copy_jobs) = B::run_thin_lto(cgcx, needs_thin_lto, import_only_modules) | ||
|
@@ -1456,6 +1462,7 @@ fn start_executing_work<B: ExtraBackendMethods>( | |
if needs_fat_lto.is_empty() | ||
&& needs_thin_lto.is_empty() | ||
&& lto_import_only_modules.is_empty() | ||
&& autodiff_items.is_empty() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Sa4dUs I am trying to get back to this PR to handle cases where users try to use autodiff in a rust library, which is not yet supported. Since the refactor we don't have the autodiff_items around anymore. Do you know of a good way of checking here whether there is still work to do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not really, what do you exactly need this for? there's prob no way to check the progress of autodiff items 'cause almost all the logic is now handled by the intrinsic |
||
{ | ||
// Nothing more to do! | ||
break; | ||
|
@@ -1469,13 +1476,14 @@ fn start_executing_work<B: ExtraBackendMethods>( | |
assert!(!started_lto); | ||
started_lto = true; | ||
|
||
let autodiff_items = mem::take(&mut autodiff_items); | ||
let needs_fat_lto = mem::take(&mut needs_fat_lto); | ||
let needs_thin_lto = mem::take(&mut needs_thin_lto); | ||
let import_only_modules = mem::take(&mut lto_import_only_modules); | ||
|
||
for (work, cost) in generate_lto_work( | ||
&cgcx, | ||
autodiff_items.clone(), | ||
autodiff_items, | ||
needs_fat_lto, | ||
needs_thin_lto, | ||
import_only_modules, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![feature(autodiff)] | ||
#![crate_type = "rlib"] | ||
//@ needs-enzyme | ||
//@ compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=fat | ||
//@ build-fail | ||
|
||
// We test that we fail to compile if a user applied an autodiff_ macro in src/lib.rs, | ||
// since autodiff doesn't work in libraries yet. In the past we used to just return zeros in the | ||
// autodiffed functions, which is obviously confusing and wrong, so erroring is an improvement. | ||
|
||
use std::autodiff::autodiff_reverse; | ||
//~? ERROR: using the autodiff feature with library builds is not yet supported | ||
|
||
#[autodiff_reverse(d_square, Duplicated, Active)] | ||
pub fn square(x: &f64) -> f64 { | ||
*x * *x | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
error: using the autodiff feature with library builds is not yet supported | ||
|
||
error: aborting due to 1 previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
error: using the autodiff feature requires using fat-lto. | ||
|
||
error: aborting due to 1 previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//@ needs-enzyme | ||
//@ no-prefer-dynamic | ||
//@ revisions: with_lto no_lto | ||
//@[with_lto] compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=fat | ||
//@[no_lto] compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=thin | ||
|
||
#![feature(autodiff)] | ||
//@[no_lto] build-fail | ||
//@[with_lto] build-pass | ||
|
||
// Autodiff requires users to enable lto=fat (for now). | ||
// In the past, autodiff did not run if users forget to enable fat-lto, which caused functions to | ||
// returning zero-derivatives. That's obviously wrong and confusing to users. We now added a check | ||
// which will abort compilation instead. | ||
|
||
use std::autodiff::autodiff_reverse; | ||
//[no_lto]~? ERROR using the autodiff feature requires using fat-lto. | ||
|
||
#[autodiff_reverse(d_square, Duplicated, Active)] | ||
fn square(x: &f64) -> f64 { | ||
*x * *x | ||
} | ||
|
||
fn main() { | ||
let xf64: f64 = std::hint::black_box(3.0); | ||
|
||
let mut df_dxf64: f64 = std::hint::black_box(0.0); | ||
|
||
let _output_f64 = d_square(&xf64, &mut df_dxf64, 1.0); | ||
assert_eq!(6.0, df_dxf64); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe pick a random (like the first) entry from this list and use its span so in case of a larger crate the user is aware of why this is being emitted. I could see it being confusing in case someone is adding cfgs to turn on/off autodiff and got sth wrong in the process
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a span to the error, but emit_error now fails with:
Do you have a recommendation for how to best emit the error? I had some issues to find the right function to get it to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @oli-obk if you by chance know the source of this error? Otherwise I can spend some more time to try it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know this error, maybe I'll see sth if you push the changes that don't work