Detoxification of large language models via regularized fine-tuning

Attribute-controlled fine-tuning can produce LLMs that adhere to policy while achieving competitive performance on general benchmarks.

Large language models (LLMs) have demonstrated impressive performance across a variety of tasks, but, as has been clear in multiple instances, they carry the risk of producing inappropriate, unsafe, or biased outputs. When generating responses, a successfully trained LLM should comply with a set of policies specified by its creator; for example, the developer may want to restrain the LLM from generating toxic responses. We refer to this as attribute control, as it regulates an attribute of the LLM output.

In a paper we presented at EMNLP 2024, we propose a novel method for training an LLM to adhere to a set of constraints while preserving its performance. We first define a successfully trained LLM as one that can satisfy the following constraints: (1) Attribute control — the LLM output adheres to a policy, defined by the creator in most cases; (2) Utility preservation — the LLM maintains performance comparable to that of the original LLM on utility benchmarks; and (3) Training efficiency — the cost of fine-tuning with attribute control is similar to that of typical fine-tuning.

Related content
Generative AI raises new challenges in defining, measuring, and mitigating concerns about fairness, toxicity, and intellectual property, among other things. But work has started on the solutions.

Our work is inspired by the classic idea of constraint-driven learning and posterior regularization, in which the model output is forced to adhere to a particular distribution. Specifically, we train an auxiliary model to control a specific output attribute — in this case, toxicity. During fine-tuning, the auxiliary model estimates the closest distribution that, given the current state of the LLM, satisfies the constraints, and it penalizes the gap between that estimate and the LLM’s current distribution.

The natural way to do this is to iteratively push the LLM closer to the feasible region of generations, making the estimation progressively more accurate. However, this approach is sequential, and it causes a significant increase in run time. In our paper, we also present a parallelized algorithm that updates the base LLM and regularizer simultaneously, based on their status in the last iteration. Empirically, parallelization achieves the same level of performance as sequential fine-tuning, and the time complexity is the same as that of typical, unregularized fine-tuning.

Detoxification.png
A comparison of sequential (left) and parallel (right) fine-tuning over three iterations.

We also explore adaptive regularization (i.e., the use of a domain-specific regularizer on related parts of the training data) to improve performance and prevent catastrophic forgetting.

Utility is preserved

In experiments, we fine-tuned Llama-7B and Falcon-7B models on a mixture corpus including ToxiGen (data containing toxic responses) and Wikitext (general corpus) in equal proportions. With the adaptive regularizer, our approach, overall, preserved performance better than the standard approaches of reinforcement learning (RL) and filtering, while meeting toxicity control standards.

Benchmark performance of Llama-7B and Falcon-7B with toxicity control

Model

ToxiGen (lower is better)

MMLU (5-shot) (higher is better)

Com. reasoning (0-shot) (higher is better)

Llama-7B

Baseline

23

35.1

75.6

Filtering

21.9

34.6

75.1

RL

15.2

33.6

73.2

NADO decoding

15.2

31.1

71.4

Ours w/o adaptive

15.2

30.4

71.9

Ours w/ adaptive

14.2

33.9

73.6

Falcon-7B

Baseline

14

27.2

76.1

Filtering

13.6

26.4

74.9

RL

9.8

25.4

74.4

NADO decoding

7.3

23.6

72.5

Ours w/o adaptive

7.1

23.1

71.8

Ours w/ adaptive

7.3

26.1

74.5

Generation quality is preserved

Sequences produced by our model were indistinguishable, in terms of quality, from those produced by the base model, when OPT-30B acted as a judge. This demonstrates that our method retains the quality of generation. Our model also outperformed models trained using filtering and RL approaches.

Win rate against baseline

Win rate

Base

Filter

RL

Ours

Base

N/A

44.3

45.1

51.4

Filtering

55.7

N/A

53.4

61.6

RL

54.9

46.6

N/A

61.3

Ours

48.6

38.4

38.7

N/A

Toxicity classification and generation

One of the most interesting aspects of our method is that it allows the LLM to learn from toxic content. In experiments, we fine-tuned Llama-7B models on a toxicity classification task using the Jigsaw dataset of toxic content. With standard supervised fine-tuning, the model’s performance on the classification task improved, but the increased exposure to toxic content made it more likely to generate toxic content itself. With our method, on the other hand, improving performance on the classification task reduced the generation toxicity.

Jigsaw performance using Llama-7B model with toxicity control

Model

API tox.

Classify ROC

Baseline

0.315

0.910

SFT (LLM loss)

0.344

0.966

Ours (LLM loss)

0.288

0.959

SFT (classification)

0.314

0.972

Acknowledgements: I would like to acknowledge our intern, Tao Meng (UCLA), who led the work on this paper, and our coauthors, Ninareh Mehrabi, Palash Goyal, Anil Ramakrishna, Aram Galstyan, Richard Zemel, Kai-Wei Chang, and Rahul Gupta, for their contributions.

Research areas

Related content

US, NY, New York
We are seeking an Applied Scientist to develop and optimize Visual Inertial Odometry (VIO) and sensor fusion systems for our intelligent robots. In this role, you will design, implement, and deploy state estimation and tracking algorithms that enable robots to understand their position and motion in real time, even in challenging and dynamic environments. You will own the full pipeline from algorithm development through embedded deployment, ensuring that perception systems run efficiently on resource-constrained robotic hardware. You will also leverage modern machine learning approaches to push the boundaries of classical perception methods, combining learned representations with geometric techniques to achieve robust, real-time performance. This is a deeply hands-on role. You will work directly with sensors, hardware, and real-world data, while prototyping, testing, and iterating in physical environments. The ideal candidate has strong foundations in VIO and sensor fusion, practical experience optimizing algorithms for embedded platforms, and familiarity with how modern deep learning is transforming perception. Key job responsibilities - Design and implement Visual Inertial Odometry algorithms for robust real-time state estimation on robotic platforms like Sprout - Develop multi-sensor fusion pipelines integrating cameras, IMUs, and other sensing modalities for accurate pose tracking - Optimize perception and tracking algorithms for deployment on embedded hardware (e.g., ARM, GPU-accelerated edge devices) under strict latency and power constraints - Apply modern ML-based perception techniques (learned features, depth estimation, neural odometry) to complement and improve classical geometric approaches - Build and maintain calibration, evaluation, and benchmarking infrastructure for perception systems - Collaborate with hardware, controls, and navigation teams to integrate perception outputs into the robot’s autonomy stack - Lead technical projects from research prototyping through production deployment
US, WA, Bellevue
Amazon Leo is a constellation of Low Earth Orbit satellites that will provide low-latency, high-speed broadband network connectivity to unserved and underserved communities around the world. We are looking for an Applied Scientist to be a founding scientist on the Engineering and R\&D team within Leo Infrastructure and IP Security. The team defends the manufacturing lines, launch sites, and global ground infrastructure behind the constellation from the most sophisticated threat actors on the planet. These requirements create open scientific problems at the intersection of agentic AI, real-time stream processing, graph-based reasoning, and behavioral analytics. You will build the science behind a neurosymbolic reasoning platform and the models that detect the behavior of sophisticated threat actors. This is an R\&D role with a production mandate, where you define the problem rather than solve a pre-scoped one, and every model, detection, and agent workflow you build becomes the system Leo's security teams use to protect the constellation. #### Export Control Requirement Due to applicable export control laws and regulations, candidates must be a U.S. citizen. Key job responsibilities - Design and implement scalable, production-grade neurosymbolic systems that integrate symbolic reasoning over graph-based knowledge representations with LLM agents to deliver reliable, verifiable security outcomes. - Design and run reinforcement learning and fine-tuning pipelines (GRPO, PPO, DPO) to optimize language models for security reasoning, triage, and detection-authoring tasks. - Build behavioral and statistical models that detect threat actor behavior, and design the evaluation frameworks that measure model performance against that behavior before trusting a model in production. - Design and build multi-agent systems that autonomously triage, enrich, and contain security events, including the constrained reasoning, safety guardrails, and validation mechanisms that make automated decisions trustworthy at scale. - Own the end-to-end science lifecycle, from research and experimentation through production deployment, defining the metrics that measure system performance and real-world security impact. - Advance the state of the art through publications at top-tier venues, patents, or open-source contributions, and shape the scientific agenda and research culture from day one. A day in the life You will move between research and production in the same week: framing an ambiguous security problem as a scientific question, prototyping an approach, and partnering with software engineers to ship it as a capability the platform runs continuously. Security engineers on your team translate threat intelligence into the adversary behaviors that matter; you build the models that detect those behaviors and evaluate model performance against them. You will obsess over the two latencies that define the platforms, the time from event to detection and the time from detection to containment action, and design agents and detections that drive both down. You will backtest candidate detections against retained telemetry, review evaluation results before a model or agent capability graduates to automated execution, and deliver scientific artifacts that ship. About the team Leo Infrastructure and IP Security protects the people, facilities, hardware, and supply chain behind a global satellite constellation. The Engineering and R&D team within this organization builds the platforms and tooling the security pillar teams operate on, moving security operations from manual triage to correlation-based detection, automated response, and agentic AI. The team is composed of applied scientists, software engineers, and security engineers working across physical and digital security domains. #### Inclusive Team Culture In Amazon Security, it's in our nature to learn and be curious. Ongoing DEI events and learning experiences inspire us to continue learning and to embrace our uniqueness. Addressing the toughest security challenges requires that we seek out and celebrate a diversity of ideas, perspectives, and voices. #### Training & Career Growth We're continuously raising our performance bar as we strive to become Earth's Best Employer. That's why you'll find endless knowledge-sharing, training, and other career-advancing resources here to help you develop into a better-rounded professional. #### Work/Life Balance We value work-life harmony. Achieving success at work should never come at the expense of sacrifices at home, which is why flexible work hours and arrangements are part of our culture. When we feel supported in the workplace and at home, there's nothing we can't achieve.
US, CA, Sunnyvale
Robots are about to step out of the movies and into our homes. If you're excited about this robotics revolution, come join Amazon's Fauna Robotics. As an Applied Scientist in this org, you'll push the state-of-the-art techniques in Robotics. You'll drive technical excellence in areas such as perception, manipulation, sim2real transfer, reinforcement learning and multi-task learning, designing novel algorithms that bridge the gap between research and real-world deployment. In this role, you will integrate hands-on technical expertise with scientific leadership, ensuring your team delivers robust solutions for dynamic real-world environments. While staying updated with the latest advancements in robotics research, you will leverage your experience and expertise to guide the team towards the most promising direction. Collaborating with a talented group of software engineers and scientists, you will bring your ideas to fruition. Deploying robots in home environments requires you to anticipate challenges not encountered in structured settings like warehouses. Additionally, consumer-grade sensors and actuators necessitate working around their limitations, requiring out-of-the-box innovative ideas. As such, you are someone who truly enjoys thinking about problems using first principles at a holistic and systems level to address the aforementioned challenges. Key job responsibilities - Lead technical initiatives in areas such as robotics foundation models, reinforcement learning and manipulation - - Design experiments to identify the limitations of current state-of-the-art models and developing new models or techniques that can surpass these models - Design and implement novel deep learning architectures that push the boundaries of what robots can understand and accomplish - Mentor fellow scientists while maintaining strong individual technical contributions - Collaborate with engineering teams to optimize and scale models for real-world applications - Influence technical decisions and implementation strategies within your area of focus - Experienced in communicating complex technical work to a non-technical audience - The ability to work with minimal guidance, be proactive and to handle ambiguity and the challenge of quickly evolving goals About the team Fauna Robotics, an Amazon company, is building capable, safe, and genuinely delightful robots for everyday life. Our goal is simple: make robots people actually want to live and interact with in everyday human spaces. We believe that future won’t arrive until building for robotics becomes far more accessible. Today, too much effort is spent reinventing the fundamentals. We’re changing that by developing tightly integrated hardware and software systems that make it faster, safer, and more intuitive to create real-world robotic products. Our work spans the full stack: mechanical design, control systems, dynamic modeling, and intelligent software. The focus is not just functionality, but experience. We’re building robots that feel responsive, expressive, and genuinely useful. At Fauna, you’ll work at the frontier of this space, helping define how robots move, manipulate, and interact with people in natural environments. It’s an opportunity to solve hard problems across hardware and software with a team focused on making robotics accessible and joyful to build. If you care about making robotics real for everyone and building systems that are as delightful as they are capable, we’re interested in hearing from you.
US, WA, Seattle
Economists at Amazon partner closely with senior management, business stakeholders, scientist and engineers, and economist leadership to solve key business problems ranging from Amazon Web Services, Prime, inventory planning, international retail, third party merchants, search, pricing, labor and employment planning, effective benefits (health, retirement, etc.) and beyond. Amazon Economists build econometric models using our world class data systems and apply approaches from a variety of skillsets – applied macro/time series, applied micro, econometric theory, empirical IO, empirical health, labor, public economics and related fields are all highly valued skillsets at Amazon. You will work in a fast moving environment to solve business problems as a member of either a cross-functional team embedded within a business unit or a central science and economics organization. You will be expected to develop techniques that apply econometrics to large data sets, address quantitative problems, and contribute to the design of automated systems around the company.
US, WA, Seattle
We are seeking an exceptional Applied Scientist, Seller Abuse Prevention, to lead the development and implementation of advanced AI solutions that will transform how we prevent bad actors from operating in our store and enable Selling Partners to start and grow their business without fear of disruption, so that customers and Selling Partners across the globe trust us and have confidence in the integrity of Amazon's store. This role will focus on building risk detection models leveraging state-of-the-art AI, including small language models, to detect and prevent abuse of Amazon's catalog worldwide. You will design, develop, and deploy scalable AI solutions to proactively detect and prevent marketplace abuse throughout the seller lifecycle. You will work with massive-scale, multi-modal datasets spanning behavioral patterns, transactional histories, and behavioral data to build detection systems that are ahead of evolving adversarial tactics. Key job responsibilities - Design and build predictive risk detection models using advanced AI techniques, including Natural Language Processing including LLMs and agents to proactively identify bad actors and prevent marketplace abuse at scale - Own the end-to-end scientific solution from risk quantification through decision optimization, determining the appropriate actions to take across varying risk levels - Develop interpretability and reasoning pipelines that provide transparent, actionable explanations for model decisions to support enforcement and seller experience - Work with risk programs across the seller lifecycle to define detection strategies, translate operational investigation patterns into automated systems, and prioritize high-impact risk areas - Partner with engineering teams to deploy models into production, define evaluation frameworks, and collaborate with operations and verification teams to measure and improve detection effectiveness A day in the life Day-to-day you can expect to: - Explore datasets to understand predictors and patterns of abuse - Work with product, program, and engineering stakeholders to build solutions into production that will last - Identify new and emerging abuse vectors as abusers get more sophisticated - Use search, graph, computer vision, NLP, and anomaly detection methodologies to automatically detect abusive actions. About the team Our team is driven by the belief that scientific innovation unlocks transformative customer experiences. We tackle hard, ambiguous problems by combining rigorous research methods with practical engineering, and we value collaboration across disciplines. We are building an inclusive environment where scientists at every stage of their career can grow, publish impactful work, and see their research come to life at Amazon scale. If you want to do science that matters, this is the place.
US, NY, New York
The Sponsored Products and Brands team at Amazon Ads is re-imagining the advertising landscape through industry leading generative AI technologies, revolutionizing how millions of customers discover products and engage with brands across Amazon.com and beyond. We are at the forefront of re-inventing advertising experiences, bridging human creativity with artificial intelligence to transform every aspect of the advertising lifecycle from ad creation and optimization to performance analysis and customer insights. We are a passionate group of innovators dedicated to developing responsible and intelligent AI technologies that balance the needs of advertisers, enhance the shopping experience, and strengthen the marketplace. If you're energized by solving complex challenges and pushing the boundaries of what's possible with AI, join us in shaping the future of advertising. As an Applied Scientist within the Brand Intelligence team, you will be responsible for improving the quality of underlying agentic solutions and their impact within the ads delivery stack. We are looking for a passionate candidate with technical expertise in agentic systems, continual learning, evaluation, information retrieval, Natural Language Processing (NLP), and Large Language Models (LLM). In addition to having hands-on experience in building ML-based solutions, an ideal candidate should be able to create and articulate a customer-centric science vision, show willingness to continuously learn about new scientific approaches, and enjoy operating in startup-like environment.
US, NY, New York
Fauna Robotics is building capable, safe, and delightful robots for everyday life, and understanding where it is, and what's around it, is fundamental to everything a robot does. A robot that lives alongside people must build and maintain a map of its environment, plan paths through cluttered and changing spaces, and do so reliably whether it's navigating a hallway for the hundredth time or entering a room it has never seen. We are hiring a Principal Applied Scientist to serve as a technical authority on how our robots understand and move through the world. You will design, prototype, and ship the core algorithms that give the robot spatial intelligence - from real-time SLAM and scene understanding to motion planning and autonomous navigation. You will set the mapping and navigation architecture that other engineers build on, shape sensor and compute decisions across robot generations, mentor the engineers and scientists working on spatial perception, and be the person teams come to when the robot is lost. Key job responsibilities - Set the long-range science roadmap and technical architecture for the robot's mapping, localization, and navigation stack, and serve as Fauna's primary technical authority on spatial intelligence - Design and implement real-time visual-inertial and/or lidar-based SLAM systems that build, update, and maintain persistent maps of home and indoor environments - Develop robust localization algorithms that allow the robot to determine its precise pose in previously mapped spaces, handling environmental changes such as moved furniture, lighting variation, and dynamic occupants - Build navigation and motion planning systems that produce safe, efficient, and natural-looking trajectories through cluttered, dynamic human spaces - Architect semantic mapping capabilities that go beyond geometry — enabling the robot to understand rooms, objects, surfaces, and traversable space at a level that supports higher-level task planning - Drive sensor selection and placement decisions for depth cameras, lidar, IMUs, and encoders in collaboration with mechanical, electrical, and industrial design teams, backed by your own benchmarking and analysis - Design the robot-specific data collection, benchmarking, and evaluation methods to validate mapping accuracy, localization robustness, and navigation safety across diverse real-world environments - Develop strategies for graceful degradation and recovery — ensuring the robot behaves safely when localization is uncertain, maps are incomplete, or sensors are occluded - Present mapping and navigation science and its tradeoffs to senior leadership and partner teams - Mentor scientists and engineers, raising the scientific bar for spatial perception across the organization through design reviews, code reviews, and hiring About the team Fauna Robotics, an Amazon company, is building capable, safe, and genuinely delightful robots for everyday life. Our goal is simple: make robots people actually want to live and interact with in everyday human spaces. We believe that future won't arrive until building for robotics becomes far more accessible. Today, too much effort is spent reinventing the fundamentals. We're changing that by developing tightly integrated hardware and software systems that make it faster, safer, and more intuitive to create real-world robotic products. Our work spans the full stack: mechanical design, control systems, dynamic modeling, and intelligent software. The focus is not just functionality, but experience. We're building robots that feel responsive, expressive, and genuinely useful. At Fauna, you'll work at the frontier of this space, helping define how robots move, navigate, and interact with people in natural environments. It's an opportunity to solve hard problems across hardware and software with a team focused on making robotics accessible and joyful to build. If you care about making robotics real for everyone and building systems that are as delightful as they are capable, we're interested in hearing from you.
CH, Zurich
Are you passionate about pushing the boundaries of what robots can do? Amazon is looking for a creative and driven Applied Scientist to help build intelligent robotic manipulation systems powered by reinforcement learning. In this role, you will research, design, and deploy machine learning solutions that enable robots to interact with the physical world in increasingly capable ways. You will tackle complex, ambiguous problems where textbook answers don't exist, working backwards from real-world needs to invent new approaches. Your work will directly influence how automation systems learn, adapt, and perform at Amazon's scale — and you will see your research come to life on real hardware. Key job responsibilities - Research and develop reinforcement learning methods for robotic manipulation, advancing the state of the art in sim-to-real transfer, contact-rich control, and adaptive grasping strategies. - Design, implement, and evaluate machine learning models and algorithms, leveraging deep learning frameworks such as PyTorch or TensorFlow to train policies that generalize across varied manipulation tasks. - Build and maintain production-quality code that integrates scientific prototypes into larger robotics systems, making thoughtful tradeoffs between complexity, performance, and maintainability. - Collaborate with cross-functional engineering and science teams to define simulation requirements, validate model performance, and iterate on solutions informed by real-world hardware feedback. - Contribute to the team's scientific roadmap by identifying new research directions, mentoring fellow scientists, and publishing results at peer-reviewed venues when appropriate. A day in the life You might start your morning reviewing overnight training runs, analyzing reward curves and failure modes from your latest reinforcement learning experiments. After a quick sync with robotics engineers, you shift to refining a simulation environment to close a gap you spotted between sim and real performance. In the afternoon, you pair with a teammate to design a new evaluation framework, then wrap up by drafting a short write-up of your findings to share with the broader team. About the team Our team sits at the intersection of machine learning research and real-world robotics, focused on teaching robots to manipulate objects with precision and adaptability. We combine reinforcement learning, physics-based simulation, and hands-on hardware validation to deliver solutions that work beyond the lab. We value intellectual curiosity, collaborative problem-solving, and a shared commitment to turning ambitious research ideas into tangible impact. If you are excited about building intelligent systems that learn from experience and operate at scale, this is the team for you.
US, CA, Sunnyvale
Amazon is looking for an Applied Scientist with a strong deep learning background to help build the training environments that teach large language models to reason, use tools, and complete complex, multi-step tasks. Key job responsibilities You will design, build, and evaluate reinforcement learning environments (RL gyms) for LLM training: realistic, verifiable task environments where models learn through interaction and feedback. This includes defining tasks and reward signals, building the infrastructure to run environments at scale, measuring how well environments transfer into model capability, and developing rigorous methods to verify environment quality and correctness. You will work closely with model training teams to understand where environments are most valuable and iterate based on what the models learn. Your work will directly shape products and services that use generative AI. About the team The Foundational AI (FAI) team has a mission to push the envelope in GenAI with LLMs, in order to provide the best-possible experience for our customers.
US, WA, Seattle
The charter of the Entertainment Economics team includes working on high-impact projects that require economics expertise and a cross-business perspective. Our team supports Amazon’s diverse entertainment businesses – especially Prime Video, Amazon Music, Amazon MGM Studios, and Gaming (Luna) – through complex causal measurements (observational and experimental) and structural modeling. Some of our team’s areas of science expertise include marketing science, long-term value measurement, and experimentation. As a central economics team not embedded in any particular business, we strive to provide objective analyses that take a customer-obsessed, pan-Amazon perspective. This role partners with the Prime Science team to evaluate and improve the way Amazon allocates Prime subscription revenue across its benefit teams, including entertainment businesses. Prime is Amazon's premiere membership program, with over 200mm members world-wide, bringing in billions in Prime subscription revenue annually. Prime is at the center of many major company decisions that impact Amazon's customers. These decisions span a variety of industries, each reflecting the diversity of Prime benefits: fast and free shipping, entertainment & digital content, reading, healthcare, and grocery. Each of these Prime benefits is supported by a different organization, and each relies on Prime subscription revenue to make critical investment decisions. Our team partners with Prime Science to ensure that these allocations are credible, actionable, and incentive compatible for every benefit. To support this work, this role will create science models, validate them, and productionize them. In addition to stellar economics expertise, this role requires systems thinking (anticipating how complex interconnected models will respond to changes in data or modeling), strategic thinking (supporting decisions that affect 200 million+ customers and set Amazon’s direction for years to come), and scientifically novel thinking (using AI and the latest science advancements to achieve move). The ideal candidate is expert in causal inference with experience working in large, complex structural modeling environments. They are a creative problem-solver, able to identify new approaches that solve multiple problems at once while minimizing churn. They think big, not limiting themselves to what’s been done before, while tactfully accounting for the costs of major changes. They can add structure to incredibly ambiguous business contexts, making defensible assumptions and delivering measurements that drive impactful business decisions. They have effective communication and the skill to translate data-driven findings into actionable insights for senior leaders. Finally, they are empathetic and constructive. Our healthy team culture is supportive and fast-paced, and we prioritize learning, growth, and helping each other to continuously raise the bar. Impact and Career Growth In today’s entertainment landscape, critical decisions are made with data and economic models. You’ll help leaders ask the right questions, and then deliver data-driven answers, helping to create the future of Amazon. You’ll help define a long-term science vision in this space and translate it into an actionable roadmap. This role combines science leadership, organizational ability, technical strength, product focus, and business understanding – a perfect recipe for career growth as an economist in tech. Key job responsibilities • Design and build econometric models, especially causal models, to measure the value of the business and its many features • Develop science products from concept to prototype to production, incorporating feedback from scientists and business partners • Independently identify and pursue new opportunities to leverage economic insights across entertainment and Prime • Write business and technical documents communicating business context, methods, and results to business leadership and other scientists • Serve as a technical reviewer for our team and related teams, including document and code reviews